Python Forum
Tkinter: Problem with storing data from one window to the other
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter: Problem with storing data from one window to the other
#1
Dear all,

I am very new to Python and Tkinter, so sorry if the question is very basic.
I cannot manage to store the value of a parameter, here "value" and to print it in a new window.
Please find the code below.
Thanks a lot for your help!

import tkinter as tk

#Initiate the values
#kLa = tk.DoubleVar()


# Create an object "window"
window = tk.Tk()
window.title("Application Test")


window.minsize(400,200)
window.grid_rowconfigure(0, weight=1)
window.grid_rowconfigure(6, weight=1)
window.grid_columnconfigure(0, weight=1)
window.grid_columnconfigure(3, weight=1)

# Function to open a new window
def open_window_value():
    value = float()
    def do_ok(event=None):
        nonlocal value
        value = entryvalue.get()
        window_value.destroy()

    def do_cancel():
        nonlocal value
        value = None
        window_value.destroy()
    window_value = tk.Toplevel(window)  # Create a new window
    window_value.title("Window value")
    window_value.geometry("250x150")  

    labelTitle = tk.Label(window_value, text="This is the window for value")
    labelTitle.grid(row=0,column=0)
    labelvalue = tk.Label(window_value,text = "Enter the value")

    labelvalue.grid(row=1, column=0, columnspan=4)
    entryvalue = tk.Entry(window_value, textvariable = value, width = 30)
    entryvalue.grid(row=2, column=0)
    tk.Button(window_value, text="ok", command=do_ok).grid(row=3, column=0)
    tk.Button(window_value, text="cancel", command=do_cancel).grid(row=3, column=1)
    entryvalue.bind("<Return>", do_ok)
    window_value.wait_window(window_value)
    return value

def open_window_Calc():
    window_Calc = tk.Toplevel(window)  # Create a new window
    window_Calc.title("Window Other Parameters")
    window_Calc.geometry("250x150")  

    tk.Label(window_Calc, text="This is a new window").grid(row=0, column=0)
    tk.Label(window_Calc, text=value).grid(row=1, column=0)
    

zoneMenu = tk.Frame(window, borderwidth=3, bg='#557788')
zoneMenu.grid(row=0,column=0)

menuvalue = tk.Button(zoneMenu, text='value', width='20', borderwidth=2, 
                         bg='gray', activebackground='darkorange',relief = tk.RAISED, command = open_window_value)


menuvalue.grid(row=0,column=0)

Calculation = tk.Button(zoneMenu, text='Calculate!', width='20', borderwidth=2, 
                        bg='gray', activebackground='darkorange',relief = tk.RAISED, command = open_window_Calc)

Calculation.grid(row=1,column=1)


def quit():
    window.destroy()
    
window.protocol('WM_DELETE_WINDOW', quit)

# Start the tkinter loop (to be put at the end)
window.mainloop()
Reply
#2
open_window_calc() cannot find a "value". Where is it defined?

Do you think this is where value is set?
    def do_ok(event=None):
        nonlocal value
        value = entryvalue.get()
        window_value.destroy()
That "value" only exists inside the open_window_value() function. The function returns the value, but your code ignores the return value. To get the value you need your button callback to look like this:
value = 0

def get_value():
    global value
    value = open_window_value()

menuvalue = tk.Button(
    zoneMenu,
    text="value",
    width="20",
    borderwidth=2,
    bg="gray",
    activebackground="darkorange",
    relief=tk.RAISED,
    command=get_value,
)
It looks like you are trying to write your own modal dialog, but you aren't doing it quite right. The value is only updated when the window closes, but opening the value window does not prevent opening the calculate window. The value window should block the program until the value is entered. This is most easily done using one of the dialogs provided by tkinter.
import tkinter as tk
import tkinter.simpledialog
import tkinter.messagebox


class MainWindow(tk.Tk):
    """Demonstrate how to pass a value from one window to another."""

    def __init__(self):
        super().__init__()
        self.title("Application Test")
        self.value = 0.0
        tk.Button(self, text="Value", width=40, command=self.get_value).pack(padx=10, pady=10)
        tk.Button(self, text="Calculate", width=40, command=self.show_value).pack(padx=10, pady=(0, 10))

    def get_value(self):
        self.value = tkinter.simpledialog.askfloat("Enter Value", "Value", initialvalue=0.0)

    def show_value(self):
        tkinter.messagebox.showinfo(title="Show Value", message=str(self.value))


MainWindow().mainloop()
If none of the provided dialogs fit you need, you can subclass Dialog and create your own.

The advantage of a dialog for entering information is it blocks the program from running until the value is entered. If you don't want blocking, programming becomes more complicated. If the window for entering a value does not block the main program, how does the main program know when the value was entered. One solution is to have the value always available and have the enter value dialog change the value. In this example the value is represented by a DoubleValue object. The same DoubleValue object is shared by both dialogs.
import tkinter as tk


class EntryWindow(tk.Toplevel):
    """A toplevel window for entering something."""
    def __init__(self, title, prompt, value):
        super().__init__()
        self.title(title)
        tk.Label(row, text=prompt).pack(side=tk.LEFT, padx=(100, 10), pady=10)
        tk.Entry(row, textvariable=value,).pack(side=tk.LEFT, padx=(5, 100), pady=10))


class MainWindow(tk.Tk):
    """Demonstrate how to pass a value from one window to another."""

    def __init__(self):
        super().__init__()
        self.title("Application Test")
        self.value = tk.DoubleVar(self, 0.0)
        tk.Button(self, text="Value", command=self.get_value).pack(padx=150, pady=10)
        tk.Button(self, text="Calculate", command=self.show_value).pack(padx=10, pady=(0, 10))

    def get_value(self):
        EntryWindow("Enter a value", "Value", self.value)

    def show_value(self):
        EntryWindow("Display a value", "Value", self.value)


MainWindow().mainloop()
You can have both windows open at the same time. Changing the value in one window changes the value in both windows.

This would be a good time for you to start learning about classes. GUI programming is much simpler when you use classes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter only storing last element/data from entry widget bertschj1 8 747 May-06-2025, 11:54 PM
Last Post: deanhystad
  LLM data I/O - for storing notes Alkanet 0 416 Dec-19-2024, 09:08 AM
Last Post: Alkanet
  Unique Tkinter Window kucingkembar 11 2,452 Aug-11-2024, 01:49 PM
Last Post: deanhystad
  Storing data in readable place user_404_lost_and_found 4 1,546 Jul-22-2024, 06:14 AM
Last Post: Pedroski55
  tkinter photo image problem jacksfrustration 5 3,863 Jun-27-2024, 12:06 AM
Last Post: AdamHensley
  add entries and labels to the window tkinter jacksfrustration 3 2,533 Oct-10-2023, 06:41 PM
Last Post: buran
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 5,835 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  how to open a popup window in tkinter with entry,label and button lunacy90 1 3,509 Sep-01-2023, 12:07 AM
Last Post: lunacy90
Bug tkinter.TclError: bad window path name "!button" V1ber 2 2,428 Aug-14-2023, 02:46 PM
Last Post: V1ber
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 2,045 Jun-13-2022, 08:59 AM
Last Post: Shena76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020