I have 2 text fields and one "select file" button. I need to use this button to select two files and fill the selected file name in its corresponding text field. I've tried the following code, but the problem with it is that I currently need to select the two files in a consecutive way. This doesn't give a good user experience, as the first file is in the first text field and the second is on the 5th text field. Accordingly, I need a way to select those files, not in a consecutive manner.
Code:
def browsefunc1():
filename =filedialog.askopenfilename(filetypes=(("rpt files","*.rpt"),("All files","*.*")))
inputtxt1.insert(tkinter.END, filename)
def browsefunc2():
filename = filedialog.askopenfilename(filetypes=(("fss files","*.fss"), ("All files","*.*")))
inputtxt5.insert(tkinter.END, filename)
def call_func():
browsefunc1() # This function happens before the next
browsefunc2()
fileButton = tkinter.Button(window,text="Select File",command=call_func)
CodePudding user response:
If you actually want is:
- call
browsefunc1()
wheninputtxt1
has the focus - call
browsefunc2()
wheninputtxt5
has the focus
Then you can use window.focus_get()
to get the widget that has the focus and then call the corresponding function based on the focused widget:
def call_func():
widget = window.focus_get()
if widget is inputtxt1:
browsefunc1()
elif widget is inputtxt5:
browsefunc2()