I have started learning python gui.Button function call doesn't update the global variable. Currently I am facing problem in below code.
from tkinter import *
root=Tk()
root.title("learning")
s=""
def change() :
global str
str="program"
button1=Button(root,text="Click me", command=change).pack()
print(s)
root.mainloop()
The value of str
does not update.
s=""
def change():
global s
s="program"
change()
print(s)
the value of s here gets printed program whereas using tkinter the value is blank.
Answer
The issue you're facing is related to how the global variable is being handled in your Tkinter
application. The key point is that the print(s)
statement is executed before the button is clicked and the change()
function is called. This means the global variable s
is still empty (""
) when the print(s)
statement is executed.
Let's go through the problem:
Your Code Explanation:
-
The Button: You have a button with
command=change
, which means that when the button is clicked, thechange()
function is called. -
Global Variable: You define a global variable
s
but you're not updating it in the main program execution. The value ofs
is only updated when the button is clicked, and that happens afterroot.mainloop()
starts the event loop. -
Print Statement: The
print(s)
statement is executed immediately, before the button is clicked and thechange()
function is called, so at the time of the print,s
is still""
(empty).
Solution:
To see the value of s
being updated correctly, you need to move the print(s)
statement inside the change()
function, or you can use a Label
widget in Tkinter to display the value of s
after the button is clicked.
Corrected Code:
from tkinter import *
root = Tk()
root.title("Learning")
s = "" # Global variable
def change():
global s
s = "program"
print(s) # Print inside change() to see the updated value
# Button that calls 'change' when clicked
button1 = Button(root, text="Click me", command=change)
button1.pack()
root.mainloop()
Key Changes:
-
Moved
print(s)
inside thechange()
function: This ensures that when the button is clicked, the global variables
is updated and printed correctly. -
Removed
.pack()
from the Button definition: Thepack()
method is called separately after the button is created. This is a cleaner way to organize code in Tkinter, but you can also chain it directly if you prefer.
What Happens Now:
- When you click the button, the
change()
function is called, which updates the global variables
and prints its value ("program"). - The
root.mainloop()
starts the Tkinter event loop, and the button becomes functional.
Optional: Display s
in a Label Widget:
If you want to see the value of s
on the GUI, you can use a Label
widget to display the value dynamically.
from tkinter import *
root = Tk()
root.title("Learning")
s = "" # Global variable
label = Label(root, text=s) # Label to display the value of 's'
label.pack()
def change():
global s
s = "program"
label.config(text=s) # Update the label with the new value of 's'
# Button that calls 'change' when clicked
button1 = Button(root, text="Click me", command=change)
button1.pack()
root.mainloop()
How This Works:
- A
Label
widget is used to display the value ofs
on the window. - When you click the button, the
change()
function updatess
and also updates the text of theLabel
usinglabel.config(text=s)
.
Now when you click the button, the value of s
will be reflected both in the console (via print
) and in the GUI.