简体   繁体   English

将文件对话框 Output 插入 Tkinter 中的条目小部件

[英]Insert File Dialog Output into Entry Widget in Tkinter

I was wondering if I could possibly get someone's assistance.我想知道我是否可以得到某人的帮助。 I am new to Tkinter and UI's in general, and want to create one using Tkinter.我是 Tkinter 和一般 UI 的新手,我想使用 Tkinter 创建一个。 The only problem I have right now is adding the path of a file to an entry widget, as shown below:我现在唯一的问题是将文件的路径添加到条目小部件,如下所示:

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()

As you may be able to see, I would like to add the file path to the String Variable 'self.my_string', which is the text variable of my entry widget.如您所见,我想将文件路径添加到字符串变量“self.my_string”,这是我的条目小部件的文本变量。 This should only be done once the import button is pressed.这只能在按下导入按钮后完成。

Since self.my_string is a StringVar , you should use self.my_string.set() to update its value:由于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)

Note that askopenfile() will open the file as well, so if you only want the filename, use askopenfilename() instead:请注意, 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM