I would like to close the File Open dialog after selecting a file. Currently with my code, I can select a file but the File Open dialog remains open until I click the 'X'. How can I close this window after I have selected a file.
Here is my code:
import sys
from tkinter import *
from tkinter.filedialog import askopenfilename
fname = "unassigned"
def openFile():
global fname
fname = askopenfilename()
if __name__ == '__main__':
b = Button(text='File Open', command = openFile).pack(fill=X)
mainloop()
print (fname)
The file dialog is closing just fine. I think what you are trying to say is that the Tkinter window you created to hold the button is not closing after you select a file from the dialog. To have it do this, you will need to restructure your program a bit.
First, you need to explicitly create a Tk window to hold the button:
root = Tk()
You should then list this window as the button's parent:
Button(root, text='File Open', command = openFile).pack(fill=X)
# ^^^^
Finally, you should call the destroy method of the root window at the end of openFile:
root.destroy()
This will cause the window to close and the Tkinter mainloop to exit.
In all, your script will look like this:
import sys
from tkinter import *
from tkinter.filedialog import askopenfilename
fname = "unassigned"
def openFile():
global fname
fname = askopenfilename()
root.destroy()
if __name__ == '__main__':
root = Tk()
Button(root, text='File Open', command = openFile).pack(fill=X)
mainloop()
print (fname)
Related
I am running a simple python script that opens a window with a button inside of it. When the user clicks the button a dialog box opens and asks the user to select a file directory. I want to pull that directory location out of the function. This is what the code looks like:
from tkinter import *
from tkinter import filedialog
def openFile():
filePath= filedialog.askdirectory()
return filePath
window = Tk()
button = Button(text="open", command=openFile)
button.pack()
window.mainloop()
I would like the file directory to be saved in a variable inside the window. I am able to print the filepath inside the function but I am unable to bring it out of the function.
Any help will be greatly appreciated
You cannot use the return value for functions added to buttons, as there is no way of picking up whatever is returned.
So instead you can write the return to a global variable, or a class variable, instead. Example with global variable below;
from tkinter import *
from tkinter import filedialog
my_path = None
def openFile():
global my_path
filePath = filedialog.askdirectory()
my_path = filePath
window = Tk()
button = Button(text="open", command=openFile)
button.pack()
window.mainloop()
You will then be able to pick up the my_path variable later in your script after clicking the button.
I have a simple application in python. When I click a button it should open up the same file multiple times. However, after two times the program won't open any more windows.
Here is my code so far for file1:
from tkinter import *
root = Tk()
root.geometry("600x600")
def newWin():
import file1
button = Button(root, text="Open Window of same file", command=newWin)
button.pack()
root.mainloop()
After clicking the button one time, it opens up the same file in a new window, but when I click the button in that window, it doesn't work. How do I fix this?
import file1 will only import file1 and execute the code inside file1 once. When import file1 is called again, nothing will happen because file1 has already been imported.
To get around this, you can put the code inside a function and call that function after importing file1:
# file1.py
import tkinter as tk
def main():
root = tk.Tk()
root.geometry('600x600')
def new_win():
import file1
file1.main()
button = tk.Button(root, text='Open Window of same file', command=new_win)
button.pack()
root.mainloop()
if __name__ == '__main__':
main()
However, importing file1 inside file1 is not a good practice and should be avoided.
The above code is just a demo of fixing the import issue. Actually you don't need to call import file1 inside file1, just call main():
# file1.py
import tkinter as tk
def main():
root = tk.Tk()
root.geometry('600x600')
button = tk.Button(root, text='Open Window of same file', command=main)
button.pack()
root.mainloop()
if __name__ == '__main__':
main()
Your button has no command parameter, otherwise, it will do nothing.
button = Button(root, text="Open Window of same file", command = newWin)
Also, change add the .py extension while importing your file:
import file1.py
When I run this script, two windows appear, one for the file selection and the Tkinter window. How can I change this so that the Tkinter window only opens after a file has been selected? Thanks
def main():
my_file = askopenfilename()
stage1()
def stage1():
master = Tk()
master.mainloop()
The window master does open only after the file dialog closure (try to change its title to check), the first window you see is the parent window of the file dialog. Indeed, the tkinter file dialogs are toplevel windows, so they cannot exist without a parent window. So the first window you see is the parent window of the file dialog.
The parent window can however be hidden using the withdraw method and then restored with deiconify:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
def main():
master = Tk()
master.withdraw() # hide window
my_file = askopenfilename(parent=master)
master.deiconify() # show window
master.mainloop()
if __name__ == '__main__':
main()
I want to launch an "Open File" dialog in Tkinter in Python 2.7.
My code starts with:
from Tkinter import Frame, Tk, BOTH, Text, Menu, END
import tkFileDialog as tkfd
import fileinput
root = Tk()
global strTab
strTab = ""
def openTab(event):
r = tkfd.askopenfilename()
strTab = unicodedata.normalize('NFKD', r).encode('ascii','ignore')
Later in the code I have:
btnLoadTab = Button(root,
text="Load Tab",
width=30,height=5,
bg="white",fg="black")
btnLoadTab.bind("<Button-1>", openTab)
btnLoadTab.pack()
root.mainloop()
When I press the button an "Open File" dialog is shown, but when I select a file it closes and the button remains "clicked".
If I later call to strTab outside of openTab, it remains equal to "".
You can find workable example here: http://www.python-course.eu/tkinter_dialogs.php
trying to make a GUI with an 'open file' button. When I run the code shown below, the open file dialog opens straight away, and not when I press the button. Why? Is there a simple way to fix this that doesn't involve using classes? (I don't currently know anything about classes and am working on a time-pressured project)
from tkinter import *
interface = Tk()
def openfile():
return filedialog.askopenfilename()
button = ttk.Button(interface, text = "Open", command = openfile())
button.grid(column = 1, row = 1)
interface.mainloop()
The code is passing the return value of the openfile function call, not the function itself. Pass the function itself by removing trailing () which cause a call.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
interface = Tk()
def openfile():
return filedialog.askopenfilename()
button = ttk.Button(interface, text="Open", command=openfile) # <------
button.grid(column=1, row=1)
interface.mainloop()