tkinter optionsmenu saving the values

Thread Starter

zazas321

Joined Nov 29, 2015
848
Hello. For my application, I have created an optionsmenu and I select some values there. The next time I open that menu, I want to be able to see the latest values that have been set:

Code:
    def settings_gui(self):
        self.innercanvas = tk.Canvas(canvas, width=500, height=500)
        self.options1 = tk.StringVar(self.innercanvas)
        self.options1.set("First") # WANT TO SHOW THE LAST UPDATED VALUE BUT IT DOES NOT EXIST AT THIS POINT
        
        self.options2 = tk.StringVar(self.innercanvas)
        self.options2.set("TWO") # default value
        
        self.options3 = tk.StringVar(self.innercanvas)
        self.options3.set("Three") # default valueC
        
        self.options4 = tk.StringVar(self.innercanvas)
        self.options4.set("fOUR") # default value   
        self.options5 = tk.StringVar(self.innercanvas)
        self.options5.set("fIVE") # default value

        w1 = tk.OptionMenu(self.innercanvas, self.options1, "one", "two", "three")
        w2 = tk.OptionMenu(self.innercanvas, self.options2, "one", "two", "three")
        w3 = tk.OptionMenu(self.innercanvas, self.options3, "one", "two", "three")
        w4 = tk.OptionMenu(self.innercanvas, self.options4, "one", "two", "three")
        w5 = tk.OptionMenu(self.innercanvas, self.options5, "one", "two", "three")
        
        self.canvas_inner_window=canvas.create_window(50, 25, anchor='nw', window=self.innercanvas)
        confirm_button = tk.Button(self.innercanvas, text="Confirm",font='Helvetica 12 bold', width=20, height=2, command = self.ok) 
        self.innercanvas.create_window(250,400,window = confirm_button)
        self.innercanvas.create_window(50,250,window = w1)
        self.innercanvas.create_window(150,250,window = w2)
        self.innercanvas.create_window(250,250,window = w3)
        self.innercanvas.create_window(350,250,window = w4)
        self.innercanvas.create_window(450,250,window = w5)
       
    
    def ok(self):
        print ("value is:" + self.options1.get())
        self.options1_new = self.options1
        print ("value is:" + self.options2.get())
        self.options2_new = self.options2
        print ("value is:" + self.options3.get())
        self.options3_new = self.options3
        print ("value is:" + self.options4.get())
        self.options4_new = self.options4
        print ("value is:" + self.options5.get())
        self.options5_new = self.options5
        
        self.innercanvas.delete("all")
        canvas.delete(self.canvas_inner_window)
I make a canvas within canvas when I click the "Settings" button. Within the settings, I wan to change some values and save them so the next time I open the settings I can see what is the current configuration:

https://ibb.co/X7Kcw5B


When I select the values, and confirm, I save them into a new variable self.options_new. Now the next time I open the settings I want to see that value instead of a default "One" "Two' and etc.

HOw can I do that?
 

jpanhalt

Joined Jan 18, 2008
11,088
You save them to a non-volatile memory, like EEPROM, and read from that or transfer those settings to another place, like user RAM.
 

Thread Starter

zazas321

Joined Nov 29, 2015
848
You save them to a non-volatile memory, like EEPROM, and read from that or transfer those settings to another place, like user RAM.
Thanks for the reply.
My main concern is what do I pass to tkinter optionmenu function:
Code:
        self.options1 = tk.StringVar(self.innercanvas)
        self.options1.set("First") # WANT TO SHOW THE LAST UPDATED VALUE BUT IT DOES NOT EXIST AT THIS POINT
        w1 = tk.OptionMenu(self.innercanvas, self.options1, "one", "two", "three")
Because everytime i close and enter the settings menu, I initialize and create a new self.options1 variable. Instead of creating a new one, I just want to create it once, and after that do not create a new just use the one that was updated. So passing self.options1 is not correct here since its gonna be a fresh variable everytime.
 
Top