Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use tkFileDialog to realize File selection, Save and path selection in Python

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Python how to use tkFileDialog to achieve file selection, saving and path selection article most people do not understand, so the editor gave you a summary of the following contents, detailed contents, clear steps, with a certain reference value, I hope you can read this article can be rewarded, let's take a look at this "how to use tkFileDialog in Python to achieve file selection, preservation and path selection" article.

Overview of using tkFileDialog to implement File selection, Save and path selection

After looking at the documentation of Tkinter, there are three types of Pop-up dialog, and now we are using tkFileDialog

There are three forms of tkFileDialog:

One is: askopenfilename (option=value, …) This is the Open dialog box.

One is: asksaveasfilename (option=value, …) This is the Save as dialog box.

The other is: askdirectory () this is the path selection dialog box.

The option parameters are as follows:

Defaultextension = s extension of the default file

Filetypes = [(label1, pattern1), (label2, pattern2), …] Set the options in the file type drop-down menu

Default path in the initialdir = D dialog box

The file name that is initialized in the initialfile = F dialog box

Parent = W parent dialog box (which window pops up on top)

Title = title of the T pop-up dialog box

If the file is selected, the full path of the file will be displayed after confirmation, otherwise the 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) # openfile 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: cause of No module named 'tkFileDialog' problem

There are problems with the versions of python2 and pyton3. The version after python3 comes with tkinter.

Verification

Import _ tkinter

Import tkinter

Tkinter._test ()

Enter the above command in python3 to verify.

Solution method

It should be written in Python2 as

From tkFileDialog import askdirectory

It should be written in python3 as

From tkinter.filedialog import askdirectory

TkColorChooser-> tkinter.colorchooser

TkCommonDialog-> tkinter.commondialog

Others can be analogous.

The above is about "how to use tkFileDialog in Python to achieve file selection, saving and path selection" of this article, I believe we all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more related knowledge content, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report