Use tkFileDialog to implement file selection, saving and path selection in Python
content
Use tkFileDialog to achieve file selection, saving and path selection
Overview
Example
ImportError: No module named 'tkFileDialog' problem
reason
verify
Solution
Use tkFileDialog to achieve file selection, saving and path selection
Overview
After reading Tkinter's documentation, there are three types of Pop-up dialogs, and now tkFileDialog is used.
tkFileDialog has three forms:
One is: askopenfilename(option=value, ...) This is the "Open" dialog
One is: asksaveasfilename(option=value, …) This is the save as dialog
The other is: askdirectory() This is the path selection dialog
The option parameters are as follows:
defaultextension = s
Default file extensionfiletypes = [(label1, pattern1), (label2, pattern2), …]
Set options in the file type drop-down menuinitialdir = D
Default path in dialoginitialfile = F
Initially displayed filename in the dialogparent = W
Parent dialog (whichever window pops up is on the top)title = T
The title of the popup dialog
If the file is selected, the full path of the file will be displayed after confirmation, otherwise an empty string will be returned if you click Cancel
Example
#coding=UTF-8 import Tkinter, Tkconstants, tkFileDialog class TkFileDialogExample(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
# options for buttons
button_opt = {"fill": Tkconstants.BOTH, "padx": 5, "pady": 5}
# define buttons
Tkinter.Button(self, text="askopenfile", command=self.askopenfile).pack(**button_opt)
Tkinter.Button(self, text="askopenfilename", command=self.askopenfilename).pack(**button_opt)
Tkinter.Button(self, text="asksaveasfile", command=self.asksaveasfile).pack(**button_opt)
Tkinter.Button(self, text="asksaveasfilename", command=self.asksaveasfilename).pack(**button_opt)
Tkinter.Button(self, text="askdirectory", command=self.askdirectory).pack(**button_opt)
# define options for opening or saving a file
self.file_opt = options = {}
options["defaultextension"] = ".txt"
options["filetypes"] = [("all files", ".*"), ("text files", ".txt")]
options["initialdir"] = "C:"
options["initialfile"] = "myfile.txt"
options["parent"] = root
options["title"] = "This is a title"
# This is only available on the Macintosh, and only when Navigation Services are installed.
#options["message"] = "message"
# if you use the multiple file version of the module functions this option is set automatically.
#options["multiple"] = 1
# defining options for opening a directory
self.dir_opt = options = {}
options["initialdir"] = "C:"
options["mustexist"] = False
options["parent"] = root
options["title"] = "This is a title"
def askopenfile(self):
"""Returns an opened file in read mode."""
return tkFileDialog.askopenfile(mode="r", **self.file_opt)
def askopenfilename(self):
"""Returns an opened file in read mode.
This time the dialog just returns a filename and the file is opened by your own code.
"""
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
return open(filename, "r")
def asksaveasfile(self):
"""Returns an opened file in write mode."""
return tkFileDialog.asksaveasfile(mode="w", **self.file_opt)
def asksaveasfilename(self):
"""Returns an opened file in write mode.
This time the dialog just returns a filename and the file is opened by your own code.
"""
# get filename
filename = tkFileDialog.asksaveasfilename(**self.file_opt)
# open file on your own
if filename:
return open(filename, "w")
def askdirectory(self):
"""Returns a selected directoryname."""
return tkFileDialog.askdirectory(**self.dir_opt) if __name__ == "__main__":
root = Tkinter.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
ImportError: No module named 'tkFileDialog' problem
reason
Version issues with python2 and pyton3. Versions after python3 come with tkinter.
verify
import _tkinter
import tkinter
tkinter._test()
Enter the above command in python3 to verify.
Solution
In Python2 it should be written as
from tkFileDialog import askdirectory
In python3 it should be written as
from tkinter.filedialog import askdirectory
tkColorChooser ------------>tkinter.colorchooser
tkCommonDialog --------------->tkinter.commondialog
Others can be deduced by analogy.
The above is my personal experience, I hope it can give you a reference, and I hope you will support the Yunhaitian tutorial a lot.
Original address: https://blog.csdn.net/weixin_44256803/article/details/86571383
0 Comments