In this example you will learn how to pass values between frames in Tkinter.
We need two pages for passing a variables and a __init__ method. With __init_method, we can pass parameters between frames.
We define global variable in __init__ and we can call the variable in frame that we want to call.
Python Tkinter Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import tkinter as tk # Create the main window root = tk.Tk() # Define a global variable global_var = 0 # Create a function to be called when the user clicks a button def increase_global_var(): global global_var global_var += 1 # Create a button that will call the function when clicked button = tk.Button(root, text="Increase global_var", command=increase_global_var) button.pack() # Create a function to open a new window def open_new_window(): # Create a new top-level window new_window = tk.Toplevel(root) # Create a label that displays the value of global_var label = tk.Label(new_window, text=global_var) label.pack() # Create a button that will open the new window when clicked button = tk.Button(root, text="Open new window", command=open_new_window) button.pack() # Run the main loop root.mainloop() |
The script you have provided creates a Tkinter GUI with two buttons. The first button, when clicked, will increase the value of a global variable global_var
by 1. The second button, when clicked, will open a new top-level window (a new window that is independent of the main window) and display the value of global_var
in a label widget.
The main loop at the end starts the Tkinter event loop, which listens for events (e.g., button clicks) and executes the appropriate event handlers (e.g., the increase_global_var
function or the open_new_window
function). The main loop will run until the main window is closed.
To pass a variable from one window to another in Tkinter, you can use a global variable or you can use the window.geometry
method to pass the variable as an argument in the window’s geometry string.
Here is an example using a global variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import tkinter as tk # Create the main window root = tk.Tk() # Define a global variable global_var = 0 # Create a function to be called when the user clicks a button def increase_global_var(): global global_var global_var += 1 # Create a button that will call the function when clicked button = tk.Button(root, text="Increase global_var", command=increase_global_var) button.pack() # Create a function to open a new window def open_new_window(): # Create a new top-level window new_window = tk.Toplevel(root) # Create a label that displays the value of global_var label = tk.Label(new_window, text=global_var) label.pack() # Create a button that will open the new window when clicked button = tk.Button(root, text="Open new window", command=open_new_window) button.pack() # Run the main loop root.mainloop() |
Here is an example using the window.geometry
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import tkinter as tk # Create the main window root = tk.Tk() # Define a global variable global_var = 0 # Create a function to be called when the user clicks a button def increase_global_var(): global global_var global_var += 1 # Create a button that will call the function when clicked button = tk.Button(root, text="Increase global_var", command=increase_global_var) button.pack() # Create a function to open a new window def open_new_window(): # Set the geometry of the new window to include the value of global_var new_window = tk.Toplevel(root) new_window.geometry(f"300x300+{global_var*10}+{global_var*10}") # Create a label that displays the value of global_var label = tk.Label(new_window, text=global_var) label.pack() # Create a button that will open the new window when clicked button = tk.Button(root, text="Open new window", command=open_new_window) button.pack() # Run the main loop root.mainloop() |
[…] by clicking a button in Tkinter. If you want to pass parameters between two frames click Tkinter Passing Variables Between Windows […]