Search code examples
pythontkinterglobal-variablestkinter-entry

How to keep values in Entry Widget after I wrote them for furthers lookback


I need to pick up some inputs values from an entry in a window but every time I come back to the window I need the values to be kept in the entry widget. I wrote a code like this:

class Mainwindows:
    def __init__(self, master):
        self.master = master
        self.mainframe = LabelFrame(self.master)
        self.sinputbutton = Button(self.mainframe, text="INPUT", command=self.openinput)
        self.sinputbutton.pack()

    def openinput(self):
        self.inputwindow = Toplevel(self.master)
        self.app = Sinput(self.inputwindow)

class Sinput:
    def __init__(self, master):
        self.master = master
        self.inputframe = Frame(self.master)
        self.stuffinput = DoubleVar()
        self.stuffinput = Entry(self.inputframe, textvariable=self.stuffinput)
        self.stuffinput.pack()
        self.okbutton = Button(self.inputframe, text="Ok", command=self.inputok)
        self.soilbutton.pack(side="right", padx=5, pady=10)
        self.inputframe.pack()

    def inputok(self):
        global f
        f = self.stuffinput.get()
        self.master.destroy()

So I put the input into the entry and then I push the okbutton, at this point the variable f is stored and the input window closes, but if I manage to return to the input window the value happens to be 0, I need it to be kept as the value I wrote at first!

UPDATED CODE

import tkinter as tk

class Mainwindows:
    def __init__(self, master):
        self.master = master
        self.mainframe = tk.Frame(self.master)
        self.mainframe.pack()

    # Create the variable in the main class
        self.inputVar1 = tk.DoubleVar()
        self.inputVar2 = tk.DoubleVar()


        self.sinputbutton = tk.Button(self.mainframe, text="INPUT", command=self.openinput)
        self.sinputbutton.pack()

    # Temp button to display value
        self.displaybutton = tk.Button(self.mainframe, text="Display", command=self.display)
        self.displaybutton.pack()

    def openinput(self):
        self.inputwindow = tk.Toplevel(self.master)

    # Pass an instance of the DoubleVar into Sinput class
        self.app = Sinput(self.inputwindow, self.inputVar1, self.inputVar2)

# Temp function to show value
    def display(self):
        print(self.inputVar.get())

class Sinput:
    def __init__(self, master, var):
        self.master = master
        self.var = var

        self.inputframe = tk.Frame(self.master)
        self.inputframe.pack()

        self.stuffinput = tk.Entry(self.inputframe, textvariable=self.var)
        self.stuffinput.pack()

        self.okbutton = tk.Button(self.inputframe, text="Ok", command=self.inputok)
    self.okbutton.pack(side="right", padx=5, pady=10)

    def inputok(self):
        self.master.destroy()

if __name__ == '__main__':
    root = tk.Tk()
    app = Mainwindows(root)
    root.mainloop()

As before I need to store the variables created by typing them into the two entrywidgets located in the input window because I'll need those values later and also need to show the values into the input window if I decide to come back for a further look!


Solution

  • The main problem you're having is that you destroy the window and then create a new one with each button click. In the below example I have the DoubleVar in the main class. This way it's not being destroyed each time. However each time a new window is created it will use the same DoubleVar therefore each Entry widget will display the current value it holds.

    Now although you can use global on the DoubleVar to use it between classes. When using classes I prefer to stay away from it. To do this without global you can just pass in the variable.

    import tkinter as tk
    
    class Mainwindows:
        def __init__(self, master):
            self.master = master
            self.mainframe = tk.Frame(self.master)
            self.mainframe.pack()
    
            # Create the variable in the main class
            self.inputVar = tk.DoubleVar()
    
            self.sinputbutton = tk.Button(self.mainframe, text="INPUT", command=self.openinput)
            self.sinputbutton.pack()
    
            # Temp button to display value
            self.displaybutton = tk.Button(self.mainframe, text="Display", command=self.display)
            self.displaybutton.pack()
    
        def openinput(self):
            self.inputwindow = tk.Toplevel(self.master)
    
            # Pass an instance of the DoubleVar into Sinput class
            self.app = Sinput(self.inputwindow, self.inputVar)
    
        # Temp function to show value
        def display(self):
            print(self.inputVar.get())
    
    class Sinput:
        def __init__(self, master, var):
            self.master = master
            self.var = var
    
            self.inputframe = tk.Frame(self.master)
            self.inputframe.pack()
    
            self.stuffinput = tk.Entry(self.inputframe, textvariable=self.var)
            self.stuffinput.pack()
    
            self.okbutton = tk.Button(self.inputframe, text="Ok", command=self.inputok)
            self.okbutton.pack(side="right", padx=5, pady=10)
    
        def inputok(self):
            self.master.destroy()
    
    if __name__ == '__main__':
        root = tk.Tk()
        app = Mainwindows(root)
        root.mainloop()
    

    As a side note you can also look towards tkinter.simpledialog.askstring if you want a message box to get user input and return the value instead of creating your own.