How to bind tkinter filedialog to Entry widget - python

I'm writing a tkinter app that involves a lot of user input fields. One of these fields is a full file path, which is just begging for user error. In order to mitigate this, I'd like to program that particular Entry widget to call filedialog.askopenfilename() when clicked on, so that the user can just pick the file rather than entering the full path manually. As I understand it, the following code should do just that:
# Minimalized version of my code that reproduces the problem
import tkinter as tk
from tkinter import ttk, filedialog
parent = tk.Tk()
val = tk.StringVar()
def get_file(_):
f = filedialog.askopenfilename()
val.set(f)
parent.focus()
label = ttk.Label(parent, text="Put your file here")
label.pack()
enter = ttk.Entry(parent, textvariable=val)
enter.pack()
enter.bind("<FocusIn>", get_file)
parent.mainloop()
When I run this and click on the Entry widget, get_file() gets called twice in a row. The second call comes immediately after I close the first file dialog and overwrites the first file I chose.
Why is this happening, and how do I stop it?
Thanks!

Related

Tkinter filedialog clearing previous input

I just started programming with Python and I'm trying to create a GUI using tkinter where it would ask the user to select a zip file and file destination to send it to after extracting. What I have noticed is that when a user re-enters a destination, it would still store the previous file directory as well. How do I prevent this?
import tkinter as tk
from tkinter import filedialog
# Setting the window size
screenHeight = 450
screenWidth = 350
root = tk.Tk()
# get the location of the zip file
def input_dir():
input_filedir = filedialog.askopenfilename(initialdir='/', title='Select File',
filetypes=(("zip", "*.zip"), ("all files", "*.*")))
my_label.pack()
return input_filedir
# get location of destination for file
def output_dir():
output_filename = filedialog.askdirectory()
# Setting the canvas size to insert our frames
canvas = tk.Canvas(root, height=screenHeight, width=screenWidth)
canvas.pack()
# Setting the frame size to insert all our widgets
frame = tk.Frame(root, bg='#002060')
frame.place(relwidth=1, relheight=1)
# button to get user to chose file directory
openFile = tk.Button(frame, text='Choose the file path you want to extract', command=input_dir)
openFile.pack(side='top')
# button to get destination path
saveFile = tk.Button(frame, text="Chose the location to save the file", command=output_dir)
saveFile.pack(side='bottom')
extractButton = tk.Button(frame, text="Extract Now")
root.mainloop()
I have tried adding this line of code in the def input_dir function but it changed the positioning of the buttons. I'm still working on the code for extracting the zip.
for widget in frame.winfor_children():
if isinstance(widget, tk.Label):
widget.destroy()
The files/directories the user clicks are not saved really. Your variables input_filedir and output_filename are garbage collected when their respective functions are completed. If you are talking about how the dialog boxes go back to the most recent places opened in a dialog, that is not really something you can mess with too much. The easy answer is you can add the keyword 'initialdir' when you make the dialog to just pick where it goes like this:
output_filename = filedialog.askdirectory(initialdir='C:/This/sort/of/thing/')
The long answer is that filedialog.askdirectory actually creates an instance of filedialog.Directory and in that class, it saves that information for later in a method called _fixresult (but that method also does other things that are important.) You could overwrite this by doing something like this:
class MyDirectory(filedialog.Directory):
def _fixresult(self, widget, result):
"""
this is just a copy of filedialog.Directory._fixresult without
the part that saves the directory for next time
"""
if result:
# convert Tcl path objects to strings
try:
result = result.string
except AttributeError:
# it already is a string
pass
self.directory = result # compatibility
return result
def askdirectory (**options):
"Ask for a directory, and return the file name"
return MyDirectory(**options).show()
and then using your own askdirectory function instead of filedialog.askdirectory, but that is really convoluted so I recommend using initialdir instead if you can.
P.S. When a button is clicked, it calls the function you set after "command=" when you made the button; it seems you got that. But these functions are 'void', in that their return is just ignored. That is to say, the "return input_filedir" line does nothing.

How to copy text from popup menu in Tkinter/Python using 1 function for multiple widgets?

I am trying to create a popup menu that opens only when right-clicked inside of certain widgets (Text and Entry, in this case) but nowhere else
inside the root window.
When a user right-clicks inside one of the widgets and selects "copy", the text selection inside that widget
should be copied to the clipboard.
As is, the code below only works when explicitly referring to a certain widget but I want to generalize the copyToClipboard function
to copy the text selection from the widget that the user right-clicked inside.
Instead, running the commented out lines from the code below gives the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\...\...\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
TypeError: <lambda>() missing 1 required positional argument: 'e'
How do I access the appropriate (right-clicked) widget within the copyToClipboard function?
def copyToClipboard():
#def copyToClipboard(event):
#value = event.widget.get(SEL_FIRST,SEL_LAST)
value = inputText.get(SEL_FIRST,SEL_LAST)
pyperclip.copy(value)
print(value)
def showMenu(event):
popup.post(event.x_root, event.y_root)
inputEntry = ttk.Entry(root)
inputText = Text(root)
popup = Menu(root)
popup.add_command(label="Copy", command=copyToClipboard)
#popup.add_command(label="Copy", command=lambda e: copyToClipboard(e))
inputText.bind("<Button-3>", showMenu)
inputEntry.bind("<Button-3>", showMenu)
inputText.pack()
inputEntry.pack()
mainloop()
I've added a solution below. Storing event.widget as a global variable seemed to help as per acw's suggestion. I got rid of pyperclip because it kept giving me chinese chars and other random chars when mixing clicking-to-copy with Ctrl-V.
EDIT: It is worth noting that the Entry widget doesn't seem to handle line breaks well when they are pasted with Ctrl-V into the entry widget. Unfortunately, I haven't found an effective way to override the hotkey's default paste command to remove the line breaks prior to pasting.
from tkinter import *
from tkinter import ttk
root = Tk()
def copyToClipboard():
val = clickedWidget.selection_get()
root.clipboard_clear()
root.clipboard_append(val)
def showMenu(event):
global clickedWidget
clickedWidget = event.widget
popup.post(event.x_root, event.y_root)
inputEntry = ttk.Entry(root)
inputText = Text(root)
popup = Menu(root)
popup.add_command(label="Copy", command=copyToClipboard)
inputText.bind("<Button-3>", showMenu)
inputEntry.bind("<Button-3>", showMenu)
inputText.pack()
inputEntry.pack()
mainloop()

Python tk entry box not active until window deselected

I have a small Tk-based application that uses a standard layout of a window, defined in init. For one of the submenu items, I need to temporarily create a small form, which I remove after it is successfully submitted. I do this on the fly with the code in start_make_canvas in the mcve below:
import random
import tkinter
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk, Image
NONE=0
TAGGING=1
MAKECANVAS=4
TAGS=["some text","some more text"]
PICS=["c:/users/rob/desktop/camera.jpg","c:/users/rob/desktop/fridge.jpg"]
class Window(Frame):
def __init__(self,master):
Frame.__init__(self, master)
self.master=master
self.mode=NONE
self.init_window()
self.start_tagging()
def start_tagging(self):
if self.photo is not None:
self.photo.destroy()
self.photo=None
messagebox.showinfo("Start tagging")
self.mode=TAGGING
self.configure_buttons()
self.show_pic()
def init_window(self):
menubar=Menu(self.master)
menu=Menu(menubar,tearoff=0)
menu.add_command(label="Start tagging",command=self.start_tagging)
menu.add_command(label="Make canvas",command=self.start_make_canvas)
menubar.add_cascade(label="Tag",menu=menu)
self.master.config(menu=menubar)
self.pack(fill=BOTH,expand=1) #take full space of root window
self.photo=None
self.tag_trk={}
row=1
for tag in TAGS:
self.tag_trk[tag]=IntVar()
Checkbutton(self,text=tag,variable=self.tag_trk[tag]).place(x=500,y=10+20*row)
row+=1
self.tag_count=StringVar()
self.button1_label=StringVar()
self.btn1=Button(self,textvariable=self.button1_label,command=self.button1_click)
self.btn1.place(x=10,y=495)
self.max_score=StringVar()
def configure_buttons(self):
if self.mode==NONE:
self.button1_label.set("Tag")
elif self.mode==TAGGING:
self.button1_label.set("Next")
elif self.mode==MAKECANVAS:
self.button1_label.set("Make")
def button1_click(self):
if self.mode==TAGGING:
self.show_pic()
elif self.mode==MAKECANVAS:
# do some things here
for e in self.form: e.destroy()
self.mode=NONE
self.configure_buttons()
elif self.mode==NONE:
self.start_tagging()
def show_pic(self):
if self.photo is not None:
self.photo.destroy()
img=ImageTk.PhotoImage(Image.open(random.choice(PICS)))
self.photo=tkinter.Label(self,image=img,borderwidth=0)
self.photo.image=img
self.photo.place(x=15,y=5)
def start_make_canvas(self):
if self.photo is not None:
self.photo.destroy()
self.photo=None
self.mode=MAKECANVAS
self.form=[]
e=Label(self,text='Max score')
e.place(x=80,y=200)
self.form.append(e)
e=Entry(self,textvariable=self.max_score,width=20)
e.place(x=180,y=200)
self.form.append(e)
self.form[1].focus_set()
self.configure_buttons()
def target_tags():
global root
root=tkinter.Tk()
root.geometry("700x570")
root.protocol("WM_DELETE_WINDOW", on_closing)
app=Window(root)
root.mainloop()
def on_closing():
global root
root.destroy()
if __name__ == "__main__":
target_tags()
The problem occurs after selecting "Make Canvas" from the menu - the form creation works just fine, except that the newly created Entry elements are not active when first created: I cannot see an insertion cursor, and typed text does not go into the entry. When I select a different window and the reselect my application window, all is fine. Is there a method I need to call after I create the form for mainloop to recognize that there are new bits to be looking after?
Note: in creating the mcve, I found that the messagebox in start_tagging was necessary to recreate the problem. Without it, everything works from the get-go. With it, the checkboxes that are created initially work fine, but the new entry box doesn't (until the window is unselected/reselected).
For some reason, the Entry control looks as if it hasn't been fully initialized yet: I cannot even click on the control and input something into it, it doesn't react. It goes to normal if I Alt-Tab out of the app and back in. Using focus_force() helps as a workaround (it's justified in this case since it's done as a response to user's affirmative action).
This could as well be a bug in Tk, or some additional step is required after .place() that place manual page "forgot" to mention. (With ttk.Entry, it's just the same, so not a case of obsolete code.) You can ask # tcl-core#lists.sourceforge.net about this.

How do I create an Import File Button with Tkinter?

So you know how when you use Notepad (on Windows), for example, and you want to open an old file? You click file, then open, then a file dialog opens ups and you can select the file you want, and the program will display its contents.
Basically, I want to make a button in Python that can do that exact thing.
Here's my function for the button-
def UploadAction():
#What to do when the Upload button is pressed
from tkinter import filedialog
When I click on the button assigned to this action, nothing happens, no errors, no crash, just nothing.
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilename()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='Open', command=UploadAction)
button.pack()
root.mainloop()

tkinter filedialog open method strange behavior

I am new to Tkinter and GUI design but I'm definitely excited to learn. I'm trying to create a user interface that allows a user to load an excel spreadsheet .xls. If the spreadsheet is successfully opened then a button should pop up allowing the user to import information into a database.
My problem is that whenever I run this code the file dialog pops up before the main gui with the actual browse button pops up. I did some tests and this behavior is due to calling fname.show(). The thing is, and please correct me if I'm wrong, I need to use fname.show() to get the filepath so that I can pass the filepath into the xlrd.open_workbook method to read data from the spreadsheet. Is there another way of getting the filename? I feel like there should be an easy solution but there isn't much in terms of documentation on the specifics of tkFileDialog.
Thanks.
Below is my code:
import config
from importdb import importTLAtoDB
import Tkinter as tk
import tkFileDialog as tkfd
import tkMessageBox as tkmb
import xlrd
def openFile():
#returns an opened file
fname = tkfd.Open(filetypes = [("xls files","*.xls")])
fpath = fname.show()
if fname:
try:
TLA_sheet = xlrd.open_workbook(fpath).\
sheet_by_name('Q3 TLA - TOP SKUs')
tk.Button(root, text = "Import TLAs", command = importTLAtoDB(TLA_sheet)).pack()
tkmb.showinfo("Success!", "Spreadsheet successfully loaded. \n\
Click Import TLAs to load TLA info into RCKHYVEDB database.")
except:
tkmb.showerror("Error", "Failed to read file\n '%s'\n\
Make sure file is a type .xls" % fpath)
#GUI setup
root = tk.Tk()
root.title("TLA Database Tool")
tk.Button(root, text = "Browse", command = openFile(), width = 10).pack()
root.mainloop()
When you do, command = importTLAtoDB(TLA_sheet) you call the function and assign functions' value to command.
If you want to call a function with an argument, you need to use lambda or partials(there might be other options but these two are most preferred ones as far as I know).
So your Button line should be like this:
tk.Button(root, text="Import TLAs", command=lambda: importTLAtoDB(TLA_sheet)).pack()
Also, you may want to check this question. How to pass arguments to a Button command in Tkinter?
tk.Button(root, text = "Browse", command = openFile, width = 10).pack()
you dont want to actually call it when you setup the button

Categories

Resources