程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Tkinter module to realize file selection function in Python

編輯:Python

Catalog

Preface

1. Import libraries and modules

2. Write button commands

3. Form initialization and layout

4. function

Preface

Study Python in , Always want to make a graphical interface , Looking around , eureka tkinter.

Exercise content : In the graphical interface , After clicking the button , Use the pop-up dialog box to select a file ( Or folder )

1. Import libraries and modules import tkinter as tkfrom tkinter import filedialog

Here are some mistakes in the exercise : In no time 2 When importing statements , Use tk.filedialog after , Prompt error , Show

Cannot find reference ‘filedialog’ in 'init.py

I checked “Lib/tkinter/" Folder , It was found that there was no tkinter.py, But there are filedialog.py
I thought :tkinter It's the library ,filedialog It's a module ,
But why tk.filedialog Out-of-service ?
Instead, , In the 2 When importing statements , use tk.filedialog and filedialog Fine

Error situation :

Normal condition :

2. Write button commands def select_file(): # Single file selection selected_file_path = filedialog.askopenfilename() # Use askopenfilename Function to select a single file select_path.set(selected_file_path) def select_files(): # Multiple file selections selected_files_path = filedialog.askopenfilenames() # askopenfilenames Function to select multiple files select_path.set('\n'.join(selected_files_path)) # The paths of multiple files are separated by newline characters def select_folder(): # Folder selection selected_folder = filedialog.askdirectory() # Use askdirectory Function to select a folder select_path.set(selected_folder)

Be careful : Three button commands , Variable select_path Is in the main form Entry The control of textvariable Property value , During form initialization , It needs to be assigned :

select_path = StringVar()3. Form initialization and layout root = tk.Tk()root.title(" Select a file or folder , Get the path ")# initialization Entry The control of textvariable Property value select_path = tk.StringVar()# Layout control tk.Label(root, text=" File path :").grid(column=0, row=0, rowspan=3)tk.Entry(root, textvariable = select_path).grid(column=1, row=0, rowspan=3)tk.Button(root, text=" Select a single file ", command=select_file).grid(row=0, column=2)tk.Button(root, text=" Select multiple files ", command=select_files).grid(row=1, column=2)tk.Button(root, text=" Select the folder ", command=select_folder).grid(row=2, column=2)root.mainloop()4. function

When a single file is selected

This is about python Use tkinter This is the end of the module's article on file selection , More about python Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you can support the software development network in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved