将文件对话框输出插入 Tkinter 中的条目小部件
Insert File Dialog Output into Entry Widget in Tkinter
我想知道是否可以得到某人的帮助。我是 Tkinter 和 UI 的新手,想用 Tkinter 创建一个。我现在唯一的问题是将文件的路径添加到条目小部件,如下所示:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import tkcalendar
import datetime
class Feedback:
def __init__(self, master):
self.frame_date = ttk.Frame(master)
self.frame_date.pack()
self.style = ttk.Style()
self.style.configure('Header.TLabel', font=('Arial', 12, 'bold'))
ttk.Label(self.frame_date, text='Quarter Start Date', style='Header.TLabel').grid(row=0, column=0, padx=40)
ttk.Label(self.frame_date, text='Quarter End Date', style='Header.TLabel').grid(row=0, column=1, padx=40)
self.calendar_start = tkcalendar.DateEntry(self.frame_date)
self.calendar_start.grid(row=1, column=0, padx=40, ipadx=20)
self.calendar_end = tkcalendar.DateEntry(self.frame_date)
self.calendar_end.grid(row=1, column=1, padx=40, ipadx=20)
self.frame_docs = ttk.Frame(master)
self.frame_docs.pack()
ttk.Label(self.frame_docs, text='Choose Counter Level File', style='Header.TLabel').grid(row=0, column=0,
columnspan=2)
self.cl_import_button = ttk.Button(self.frame_docs, text='Import Counter Level',
command=lambda: self.paste_file_name()).grid(row=1, column=0, ipadx=40) #the button pressed to open up the file dialog
self.my_string = StringVar() #string variable used to hold file dialog input
self.cl_filepath = ttk.Entry(self.frame_docs, textvariable=self.my_string).grid(row=2, column=0) #the entry widget used to hold the file path
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfile()
self.my_string = self.file_name
def main():
root = Tk()
feedback = Feedback(root)
root.mainloop()
if __name__ == '__main__': main()
如您所见,我想将文件路径添加到字符串变量 'self.my_string',这是我的条目小部件的文本变量。这应该只在按下导入按钮后完成。
由于 self.my_string
是 StringVar
,您应该使用 self.my_string.set()
来更新它的值:
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfile()
self.my_string.set(self.file_name.name)
请注意 askopenfile()
也会打开文件,因此如果您只需要文件名,请改用 askopenfilename()
:
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfilename()
self.my_string.set(self.file_name)
我想知道是否可以得到某人的帮助。我是 Tkinter 和 UI 的新手,想用 Tkinter 创建一个。我现在唯一的问题是将文件的路径添加到条目小部件,如下所示:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import tkcalendar
import datetime
class Feedback:
def __init__(self, master):
self.frame_date = ttk.Frame(master)
self.frame_date.pack()
self.style = ttk.Style()
self.style.configure('Header.TLabel', font=('Arial', 12, 'bold'))
ttk.Label(self.frame_date, text='Quarter Start Date', style='Header.TLabel').grid(row=0, column=0, padx=40)
ttk.Label(self.frame_date, text='Quarter End Date', style='Header.TLabel').grid(row=0, column=1, padx=40)
self.calendar_start = tkcalendar.DateEntry(self.frame_date)
self.calendar_start.grid(row=1, column=0, padx=40, ipadx=20)
self.calendar_end = tkcalendar.DateEntry(self.frame_date)
self.calendar_end.grid(row=1, column=1, padx=40, ipadx=20)
self.frame_docs = ttk.Frame(master)
self.frame_docs.pack()
ttk.Label(self.frame_docs, text='Choose Counter Level File', style='Header.TLabel').grid(row=0, column=0,
columnspan=2)
self.cl_import_button = ttk.Button(self.frame_docs, text='Import Counter Level',
command=lambda: self.paste_file_name()).grid(row=1, column=0, ipadx=40) #the button pressed to open up the file dialog
self.my_string = StringVar() #string variable used to hold file dialog input
self.cl_filepath = ttk.Entry(self.frame_docs, textvariable=self.my_string).grid(row=2, column=0) #the entry widget used to hold the file path
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfile()
self.my_string = self.file_name
def main():
root = Tk()
feedback = Feedback(root)
root.mainloop()
if __name__ == '__main__': main()
如您所见,我想将文件路径添加到字符串变量 'self.my_string',这是我的条目小部件的文本变量。这应该只在按下导入按钮后完成。
由于 self.my_string
是 StringVar
,您应该使用 self.my_string.set()
来更新它的值:
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfile()
self.my_string.set(self.file_name.name)
请注意 askopenfile()
也会打开文件,因此如果您只需要文件名,请改用 askopenfilename()
:
def paste_file_name(self): #the function called to open up the file dialog and save the path
self.file_name = filedialog.askopenfilename()
self.my_string.set(self.file_name)