在 Tkinter 中动态设置条目小部件值

Set Entry widget values dynamically in Tkinter

我有一个 window 可以浏览包含必要文件的文件夹。我正在使用 tkFileDialog 进行相同的操作。我想将 Entry 小部件的值设置为等于此 selected 文件夹。最初,当没有文件夹被 selected 时,它将为空。一旦我 select 文件夹, selected 文件夹的路径应该出现在条目小部件中。用户应该可以modify.Below提到的是相同的代码。

from Tkinter import *
from tkFileDialog import *

class Checkit:
    root = Tk()
    #default string to be displayed in the entry of path        
    path_to_file = StringVar(root, value="abc")

    def __init__(self):     
    self.inputDetail()

    def inputDetail(self):              
        #copy the root window
        master = self.root

        #create frame for details in the root window
        details_frame = Frame(master)
        details_frame.grid(row=0, column=0)

        #Create the Labels
        papercode_label = Label(details_frame, text="Paper code:")
        subject_label = Label(details_frame, text="Subject:")
        chapter_label = Label(details_frame, text="Chapter:")
        batch_label = Label(details_frame, text="Batch:")
        ansFolder_label = Label(details_frame, text="Folder containing answer-keys:")

        #create entry for the labels
        papercode_entry = Entry(details_frame)
        subject_entry = Entry(details_frame)
        chapter_entry = Entry(details_frame)
        batch_entry = Entry(details_frame)
        ansFolder_entry = Entry(details_frame)

        #create button to add path
        path = Button(details_frame, text="Browse", command= lambda: self.addpath(details_frame))

        #button to enter the next window
        next = Button(details_frame, text="Next", command= lambda: self.checkOmr(details_frame, master))

        #Use grid layout to place labels and entry
        papercode_label.grid(row=0, sticky=W)
        papercode_entry.grid(row=1, sticky=W)

        subject_label.grid(row=2, sticky=W)
        subject_entry.grid(row=3, column=0, sticky=W)

        chapter_label.grid(row=4, sticky=W)
        chapter_entry.grid(row=5, column=0, sticky=W)

        batch_label.grid(row=6, sticky=W)
        batch_entry.grid(row=7, column=0, sticky=W)

        ansFolder_label.grid(row=8, sticky=W)
        path.grid(row=9, sticky=W, columnspan=2)
        next.grid(row=10, sticky=E, columnspan=2)

        master.mainloop()

    def checkOmr(self, old_frame, master):
        #destoy the  old frame
        old_frame.destroy()

        #create frame for details in the root window
        inputPath_frame = Frame(master)
        inputPath_frame.grid(row=0, column=0)

        #create label to input folder containing 
        omrFolder_label = Label(inputPath_frame, text="Folder containing OMR sheet to be checked:")     

        #create button to add path
        path = Button(inputPath_frame, text="Browse", command= lambda: self.addpath(inputPath_frame))

        selected_path = Entry(inputPath_frame, textvariable=self.path_to_file)

        #place the label and button on the grid     
        omrFolder_label.grid(row=0, sticky=W)       
        path.grid(row=1, column=0, sticky=W)
        selected_path.grid(row=1, column=1, sticky=W)

        #master.mainloop()

    def addpath(self, details_frame):
        self.path_to_file = askdirectory(parent=details_frame,initialdir="/",title='Please select a directory')     

if __name__=='__main__':
    handle = Checkit()

这里我尝试改变修改自我。 path_to_file 单击按钮时的值。我试图在 addpath() 中打印 self.path_to_file 值的值。它在那里给出了正确的结果,但是 checkOMR() 中条目 selected_path 中的值没有修改。

有人可以建议我应该做哪些改变才能使这件事成为可能。

看看这一行:

self.path_to_file = askdirectory(...)  

在这行代码运行s之前,self.path_to_file是一个StringVar的实例。这行代码有运行后,self.path_to_file被重置为字符串

假设您希望 self.path_to_file 保留 StringVar 的实例,您需要将该行更改为:

path = askdirectory(...)
self.path_to_file.set(path)