2

I'm trying to make some GUI on Python3 with tkinter. So far I have Main Window and 'Test' button on it, which opens second window. Second window has entry, label, save and close buttons. When you type something in entry and press save button, label shows the text you typed in entry. But after closing this window and opening it again, label shows nothing. How do I make this label to show the text that were typed last time before closing? For example, I type 'Hi' in entry, press 'Save', then I press 'Close', then I open this window again and label shows 'Hi'

import tkinter as tk

def save_data(entry, t):
    t.config(text = entry.get())

def close_action(current_window):
    current_window.destroy()

def insertMainInfo():
    new_window = tk.Tk()
    new_window.geometry("307x131")
    new_window.title("TestWindow")

    test_entry = tk.Entry(new_window)
    test_entry.place(relx = 0.283, rely = 0.1, height = 24, width = 127)

    text = tk.Label(new_window)
    text.place(relx = 0.283, rely = 0.25, height = 24, width = 127)

    save_button = tk.Button(new_window, command = lambda: save_data(test_entry, text))
    save_button.place(relx=0.283, rely=0.45, height=24, width=127)
    save_button.configure(text = "Save")

    close = tk.Button(new_window, command = lambda: close_action(new_window))
    close.place(relx=0.283, rely=0.687, height=24, width=127)
    close.configure(text = "Close")

    new_window.mainloop()

if __name__ == '__main__':

    top = tk.Tk()
    top.geometry("307x131+557+330")
    top.resizable(width=False, height=False)
    top.title("MainWindow")

    new_window_button = tk.Button(top, command = insertMainInfo)
    new_window_button.place(relx=0.283, rely=0.687, height=24, width=127)
    new_window_button.configure(text = "Test")

    main_label = tk.Label(top)
    main_label.place(relx=0.033, rely=0.153, height=41, width=284)
    main_label.configure(text = "TestLabel")

    top.mainloop()
  • In general saving data involves writing the data to your disk for later retrieval. See [reading/writing file](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). – r.ook Mar 26 '19 at 18:16
  • 1
    Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Mar 26 '19 at 19:05
  • You can create a `StringVar` and pass it as a argument of `insertMainInfo()`. Then set the `textvariable` option to this variable for both label and entry. – acw1668 Mar 26 '19 at 23:16

1 Answers1

1

I have to confess that your question heading is a bit ambiguity. If you just want to update a label of last entry, here a simple way of modifying your code.

As advised, as a good practice we only have one Tk() in a program, other new windows or pop-up windows should use Toplevel() of tkinter class; So I use this in your insertMainInfo() function.

The point here is to define a variable, I called last_entry and initially is empty or ‘’. Use this as parameter in new_window button in main program (after if __name__ == '__main__': ) to this variable (I also add lambda function here).

Then we define it as global in save_data function, so as it can be known later by other functions or main program as the last entry before new_window is closed.

Here I modify your code as said above, and I have tested it, and it works as expected.

import tkinter as tk

def save_data(entry, t):
    global last_entry
    last_entry = entry.get()
    t.config(text = last_entry)

def close_action(current_window):
    current_window.destroy()

def insertMainInfo(last_entry):
    new_window = tk.Toplevel()
    new_window.geometry("307x131")
    new_window.title("TestWindow")

    test_entry = tk.Entry(new_window)
    test_entry.place(relx = 0.283, rely = 0.1, height = 24, width = 127)

    text = tk.Label(new_window, text=last_entry)
    text.place(relx = 0.283, rely = 0.25, height = 24, width = 127)

    save_button = tk.Button(new_window, command = lambda: save_data(test_entry, text))
    save_button.place(relx=0.283, rely=0.45, height=24, width=127)
    save_button.configure(text = "Save")

    close = tk.Button(new_window, command = lambda: close_action(new_window))
    close.place(relx=0.283, rely=0.687, height=24, width=127)
    close.configure(text = "Close")

    new_window.mainloop()
    
# --- A Simple Data Structure ---
last_entry = ''


if __name__ == '__main__':

    top = tk.Tk()
    top.geometry("307x131+557+330")
    top.resizable(width=False, height=False)
    top.title("MainWindow")

    new_window_button = tk.Button(top, command = lambda: insertMainInfo(last_entry))
    new_window_button.place(relx=0.283, rely=0.687, height=24, width=127)
    new_window_button.configure(text = "Test")

    main_label = tk.Label(top)
    main_label.place(relx=0.033, rely=0.153, height=41, width=284)
    main_label.configure(text = "TestLabel")
    
    top.mainloop()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
KokoEfraim
  • 198
  • 8