简体   繁体   English

Python Ttk Entry 小部件从不显示文本变量值

[英]Python Ttk Entry widget never displaying textvariable value

Trying to use textvariable to set the text of a python tkinter/ttk entry widget.尝试使用textvariable设置 python tkinter/ttk 条目小部件的文本。 The value of the StringVar is correctly set, but the text still fails to appear in the entry widget. StringVar的值设置正确,但文本仍然无法出现在条目小部件中。

In this question, Cannot make default value display for for ttk entry widget using the textvariable parameter , the solution was to make the textvariable a class variable using self, however in my code the issue still persists.在这个问题中, Cannot make default value display for for ttk entry widget using the textvariable parameter ,解决方案是使用 self 使 textvariable 成为类变量,但是在我的代码中问题仍然存在。 I have presented an example of my code (this class is being executed by another):我已经展示了我的代码示例(这个类正在由另一个执行):

from tkinter import *
from tkinter import ttk
from tkinter import filedialog

class example: 

    def __init__(self): 
        pass
        
    def __call__(self):
        self.file1 = StringVar(value = "default value")
        self.file2 = StringVar(value = "default value")

        def select_file_1():
             self.file1.set(filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetype = (("txt", "*.txt"), ("All Files","*.*"))))
        def select_file_2():
            self.file2.set(filedialog.askopenfilename())
        
        root = Tk()
        root.title("example program")

        mainframe = ttk.Frame(root, padding="3 3 12 12")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        root.columnconfigure(0, weight=1)
        root.rowconfigure(0, weight=1)

        style = ttk.Style(root)
        style.configure("TLabel", font = ("TkDefaultFont", 12))

        ttk.Button(mainframe, text="Select file 1: ", command=select_file_1).grid(column=1, row=1, sticky=(W))
        self.file1Entry = ttk.Entry(mainframe, width = 60, textvariable=self.file1).grid(column=2, row=1, sticky=(E))
        ttk.Button(mainframe, text="Select file 2: ", command=select_file_2).grid(column=1, row=2, sticky=(W))
        self.file2Entry = ttk.Entry(mainframe, width = 60, textvariable=self.file2).grid(column=2, row=2, sticky=(E))

        for child in mainframe.winfo_children(): 
            child.grid_configure(padx=5, pady=5)

        root.mainloop()

Update: issue still persists when line 15 and 17 are fixed to self.file1.set(filedialog.askopenfilename())更新:当第 15 行和第 17 行固定为self.file1.set(filedialog.askopenfilename())时,问题仍然存在

Bryan is correct.布莱恩是正确的。 You're overwriting self.file1 and self.file2 with a different object instead of setting their values.您正在用不同的对象覆盖self.file1self.file2而不是设置它们的值。

def __call__(self):
    self.file1 = StringVar(value = "default value")
    self.file2 = StringVar(value = "default value")

    def select_file_1():
        file1_selection = filedialog.askopenfilename(
            initialdir="/",
            title="Select a File",
            filetype=(("txt", "*.txt"), ("All Files","*.*"))
        )
        # store the value of from the dialog in the file1 variable via 'set()'
        self.file1.set(file1_selection)
  
  
    def select_file_2():
        file2_selection = filedialog.askopenfilename()
        # store the value of from the dialog in the file2 variable via 'set()'
        self.file2.set(file2_selection)

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

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