in python's default editor, IDLE, it is possible to have multiple 'Open' dialogs opened at the same time.
I'm looking at their source, but I can't find where I can have this behavior replicated. from their IOBinding.py's :
from tkinter import filedialog as TkFileDialog
...
class IOBinding:
...
def askopenfile(self):
dir, base = self.defaultfilename("open")
if not self.opendialog:
self.opendialog = tkFileDialog.Open(master=self.text,
filetypes=self.filetypes)
filename = self.opendialog.show(initialdir=dir, initialfile=base)
return filename
so they do use tkinter's built-in filedialog module, but I can't find way to have some 'modeless' dialogs. I can have dialogs opened by two codes, which are basically same:
from tkinter import filedialog as tkFileDialog
file_name = tkFileDialog.Open( ... ).show()
file_name = tkFileDialog.askopenfilename()
but they blocks whole application - users cannot switch windows or issue new command until they close the dialog. also, I can't call these dialog functions from different thread - that will kill whole my Tk app. What should I do?
filedialog have parent option. You can change it to hidden window to prevent blocking the root window:
from tkinter import filedialog as tkFileDialog
from tkinter import *
def ask_open():
p = hidden if attach_to_hidden.get() else root
tkFileDialog.Open(parent=p).show()
root = Tk()
hidden = Toplevel()
hidden.withdraw()
attach_to_hidden = IntVar()
Checkbutton(root, text='Attach to hidden window', variable=attach_to_hidden).pack()
Button(root, text='Open', command=ask_open).pack()
root.mainloop()
Related
I am using tkinter asopenfilename to trigger a file chooser so as to read files from my local directory. My problem is that after the file is chosen, the window freezes and python is 'not responding'.
I've tried the answer from this post: Tkinter askopenfilename() won't close no luck.
Below is my code:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
root.update()
filename = askopenfilename()
print(filename)
Is there anything that I am missing? Let me know if you need more clarity. Thanks!
I tried all above solutions but didn't seem to solve the same issue for me. The dialog box was opening but somewhere in the background.
Found this code elsewhere and it works like a charm for me. On windows 10 too, python 3.x, and using Jupyter Notebook.
Posting it here in case it could help others.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
file_path = filedialog.askopenfilename(
%gui tk
print(file_path)
I tried using root.update() after filename = askopenfilename() in my MacOS.
Following worked for me-
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
filename = askopenfilename()
root.update()
print(filename)
I had the same behavior on MacOS and adding the iPython magic %gui tk appears to be solving the issue:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
%gui tk
root = Tk()
root.withdraw()
filename = askopenfilename(multiple=True)
print(filename)
From the docs:
%gui tk # enable Tk event loop integration
askopenfilename doesn't work in windows
from tkinter import *
#from tkFileDialog import askopenfilename
import tkinter.filedialog
def callback():
name= tkinter.filedialog.askopenfilenames()
print (name)
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
I am using python 3.6 and have the following issue.
These lines of codes apparently open two windows, but I only expect one to be opened.
from tkinter.filedialog import asksaveasfilename
file_name = asksaveasfilename()
root.withdraw()
IIRC, you're supposed to call withdraw before you call asksaveasfilename. Same as in Choosing a file in Python with simple Dialog.
import tkinter
from tkinter.filedialog import asksaveasfilename
tkinter.Tk().withdraw()
file_name = asksaveasfilename()
when you do filename = asksaveasfilename() you call the function
and that makes tkinter open the second window put that in a function and on
a button click the second window will open
from tkinter.filedialog import asksaveasfilename
import tkinter
root = tkinter.Tk()
root.withdraw()
asksaveasfilename()
I want to make a Tkinter interface for a program I just wrote. The interface needs to have a widget where the user can insert an image for the program to process. I couldn't find such a widget online. How would I make such an interface?
Below is an example of tkinter entry widget that takes in images right away:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
import tkinter.filedialog as tkfd
except ImportError:
import Tkinter as tk
import tkFileDialog as tkfd
if __name__ == '__main__':
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
path = tkfd.askopenfilename(initialdir = "/", title = "Select file",
filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
entry.insert('0', path)
root.mainloop()
We're trying to store a directory path in a variable using Tkinter's tkFileDialog, and it won't work (details later).
from Tkinter import *
import os
from tkFileDialog import askopenfilename, askdirectory
# Create the window
root = Tk()
# Application title & size
root.title("Title")
root.geometry("1000x600")
# Creating frame to add things to
app = Frame(root)
app.grid() # Adding app frame to grid
# Method that opens file chooser
# Gets used when button is clicked (command)
def openFileBox():
directoryPicked = tkFileDialog.askdirectory()
#easygui.fileopenbox()
for filePicked in os.listdir(directoryPicked):
if filePicked.lower().endswith(".jpg") or filePicked.lower().endswith(".gif") or filePicked.lower().endswith(".png"):
print filePicked
#TODO: add button 'Select Folder'
loaderButton = Button(app)
loaderButton["text"] = "Select Folder"
loaderButton["command"] = openFileBox
loaderButton.grid()
# Tells the program to run everything above
root.mainloop()
So what needs to happen? The way we see it (and we're beginners looking for feedback here), it should be running the openFileBox method when the button is pressed. When the method runs, it should store a selected directory to directoryPicked and print it to the console just to be sure it's working, but when we press the button it simply says 'tkFileDialog' is not defined.
Any thoughts?
It's because you're only importing askopenfilename, askdirectory from tkFileDialog you're not actually importing tkFileDialog itself
So you need to change directoryPicked = tkFileDialog.askdirectory() to directoryPicked = askdirectory()
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