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

Time:04-13

I'm quite new to Python and have a problem which I can't solve.

I want to write a Class which displays a configuration file (dict) in a tkinter frame. It should choose a tk.widged by type of value and for bool values create checkboxes, and for int and strings it should create entries.

After all it should give back a dict with the same keys, but the values changed to tk.*Var() However, with the checkboxes there is no problem. But the int and strings from the entries will not be written through the save_config() function. It seems the tk.IntVar() and the tk.StringVar() don't update when typing something in the entries.

Hope my question is clear. Can anybody please help?

code updated like asked by Bryan Oakley:

It also contains the changes suggested by Martin Finke which brings the solution for me!!

    #!/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()

Thanks to enyone!

CodePudding user response:

I suggest a simple change in the __ init __ function of the ConfigurationFrame class:

line self.configuration[name] = entry_int_txt replace self.configuration[name] = self.entry_int

line self.configuration[name] = entry_text replace self.configuration[name] = self.entry_str

  • Related