Programming Code Examples

Tkinter Passing Variables Between Windows4 min read

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:

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:

Here is an example using the window.geometry method:

Add comment