正在获取要填充到条目字段widg中的文件路径

2024-10-01 07:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我正在编写的GUI代码。作为Python的新手,我花了很长时间才达到这一点。我的困境:我点击选择文件按钮后,我可以浏览windows文件资源管理器,然后选择一个文件。我想在windows文件资源管理器中单击“打开”后,将文件路径填充到entry\u小部件中。任何指导都将不胜感激

from tkinter import *
from tkinter import filedialog

#Defining all sub-routines. Must be directly after import.
def selectfile():
    filename = filedialog.askopenfilename()

#Make the GUI.
root = Tk()

#Inserting a title for the dialog window.
root.title("Duplicate Files Tool")

#Adjusting the geometry of window to fit the title of the dialog window.
root.geometry('800x500')

#Setting the background color.
root.configure(bg='#004400')

#Adding a frame for widgets.
ButtonFrame1=Frame(root, bg='#004400')
ButtonFrame1.grid(row=1, column=0, padx=20, pady=5)

#Creating buttons for GUI interface.
Button(ButtonFrame1, text="Select A File Folder", command=selectfile, fg="blue").grid(row=1, column=0, pady=5)
Button(ButtonFrame1, text="Select A File Folder", command=selectfile, fg="blue").grid(row=2, column=0, pady=5)
Button(ButtonFrame1, text="Select Output", command=selectfile, fg="blue").grid(row=3, column=0, pady=5, sticky=W)
Button(ButtonFrame1, text="Run Tool", fg="blue").grid(row=4, column=0, pady=5, sticky=W)

#Adding entry field.
entry_field1 = Entry(ButtonFrame1, width=100, borderwidth=2).grid(row=1, column=1, padx=10, pady=5)
entry_field2 = Entry(ButtonFrame1, width=100, borderwidth=2).grid(row=2, column=1, padx=10, pady=5)
entry_field3 = Entry(ButtonFrame1, width=100, borderwidth=2).grid(row=3, column=1, padx=10, pady=5)

#EVERYTHING GOES ABOVE THIS LINE!!!
#This keeps the pop-up window/tool from closing. Creates infinite loop so window stays open.
root.mainloop()

#Notes
#.pack() throws your label or box anywhere into the data frame.

Tags: 文件thetextcolumnbuttonrootwindowgrid