字典中的 Python tk.StringVar() 不会随条目改变

Python tk.StringVar() in a dict won`t change with entry

我是 Python 的新手,有一个我无法解决的问题。

我想写一个 Class 在 tkinter 框架中显示配置文件 (dict)。它应该根据值的类型选择 tk.widged,对于 bool 值创建复选框,对于 int 和字符串它应该创建条目。

毕竟它应该返回一个具有相同键的字典,但值更改为 tk.*Var() 但是,使用复选框没有问题。 但是条目中的 int 和字符串不会通过 save_config() 函数写入。 在条目中输入内容时,tk.IntVar() 和 tk.StringVar() 似乎没有更新。

希望我的问题很清楚。 有人可以帮忙吗?

按照 Bryan Oakley 的要求更新了代码:

它还包含 Martin Finke 建议的更改,这为我带来了解决方案!!

    #!/usr/bin/env python3.9
    
    import tkinter as tk
    from tkinter import ttk
    
    
    class MainFrame(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self)
    
            self.test_dict = {"name": "Ares"}
    
            self.columnconfigure(0, weight=1)
            self.rowconfigure(0, weight=0)
    
            self.save_config_button = ttk.Button(
                self, text="Print Configuration", command=self.save_config)
            self.save_config_button.grid(row=0, column=0, padx=5, pady=5)
    
            self.configuration_frame = ConfigurationFrame
            self.config_frame = self.configuration_frame(
                self, "Configurations", self.test_dict)
            self.config_frame.grid(row=1, column=0, padx=5, pady=5)
    
            self.configuration = self.config_frame.get_configuration()
    
        def save_config(self):
            conf = {}
            for key, value in self.configuration.items():
                conf[key] = value.get()
            print(conf)
    class ConfigurationFrame(tk.LabelFrame):
        def __init__(self, parent, widget_name, config, * args, **kwargs):
            tk.LabelFrame.__init__(
                self, parent, * args, **kwargs)
    
            self.config(bd=2, text=widget_name)
            self.grid(sticky=tk.NSEW)
    
            self.configuration = {}
    
            self.rowconfigure(0, weight=0)
            self.columnconfigure(0, weight=1)
    
            count = 0
            for key, value in config.items():
                if type(value) == str:
                    name = key
                    entry_text = tk.StringVar(self)
                    entry_text.set(value)
    
                    frame_label = tk.Frame(
                        self, height=1)
                    frame_label.grid(
                        row=count, column=0, padx=10, pady=2, sticky=tk.W)
    
                    self.entry_str = ttk.Entry(
                        frame_label, textvariable=entry_text, text=name, width=10)
                    self.entry_str.grid(row=0, column=0, padx=5,
                                        pady=2, sticky=tk.W)
                    self.entry_str.insert(tk.END, str(value))
    
                    self.name_label = tk.Label(
                        frame_label, text=name)
                    self.name_label.grid(row=0, column=1, padx=5,
                                         pady=2, sticky=tk.W)
    
                    self.configuration[name] = self.entry_str
    
                count += 1
    
        def get_configuration(self):
            return self.configuration
    
    
    def main():
        MainFrame().mainloop()
    
    
    if __name__ == "__main__":
        main()

感谢大家!

我建议对 ConfigurationFrame 的 __ init __ 函数进行简单更改 class:

self.configuration[名称] = entry_int_txt 替换 self.configuration[名称] = self.entry_int

self.configuration[名称] = entry_text 替换 self.configuration[名称] = self.entry_str