This is a simple program using tkinter. It's basically supposed to display whatever the user types in the inp field and display it as a label on clicking a button. Here I have tried containing the tkinter value as a global variable and then using it in change_label():
from tkinter import *
def change_label():
global new_text
my_label['text'] = new_text
window = Tk()
window.title("My first GUI program")
window.minsize(width=500, height=300)
my_label = Label(text="This is a label.", font=('Arial', 24, 'bold'))
my_label.pack()
button = Button(text="Click me!", command=change_label)
button.pack()
inp = Entry(width=10)
inp.pack()
new_text = inp.get()
window.mainloop()
But on running, clicking the button results in showing an empty label.
However, if I declare new_text inside change_label(), the code works fine.
from tkinter import *
def change_label():
new_text = inp.get()
my_label['text'] = new_text
window = Tk()
window.title("My first GUI program")
window.minsize(width=500, height=300)
my_label = Label(text="This is a label.", font=('Arial', 24, 'bold'))
my_label.pack()
button = Button(text="Click me!", command=change_label)
button.pack()
inp = Entry(width=10)
inp.pack()
window.mainloop()
Why does the first code not work while the second does?
first off you should never declare a global var inside a function that wont be global until its called at least once
from tkinter import *
global new_text
def change_label():
my_label['text'] = new_text
window = Tk()
window.title("My first GUI program")
window.minsize(width=500, height=300)
my_label = Label(text="This is a label.", font=('Arial', 24, 'bold'))
my_label.pack()
button = Button(text="Click me!", command=change_label)
button.pack()
inp = Entry(width=10)
inp.pack()
new_text = inp.get()
window.mainloop()
first off you should never declare a global var inside a function that wont be global until its called at least once
from tkinter import *
global new_text
def change_label():
new_text = inp.get()
print(new_text)
my_label['text'] = new_text
window = Tk()
window.title("My first GUI program")
window.minsize(width=500, height=300)
my_label = Label(text="This is a label.", font=('Arial', 24, 'bold'))
my_label.pack()
button = Button(text="Click me!", command=change_label)
button.pack()
inp = Entry(width=10)
inp.pack()
window.mainloop()
Used StringVar
to helps you manage the value of a widget such as a Label
or Entry
more effectively than Global
. I added textvariable
in Entry
widget. Also, rearranged code to read most readability
The first code:
from tkinter import *
window = Tk()
window.title("My first GUI program")
window.minsize(width=500, height=300)
var1= StringVar()
def change_label():
my_label.config(text=var1.get())
my_label = Label(window, text="This is a label.", font=('Arial', 24, 'bold'))
my_label.pack()
button = Button(text="Click me!", command=change_label)
button.pack()
inp = Entry(window, textvariable= var1, width=10)
inp.pack()
window.mainloop()
Result before clicking:
Result after clicking:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.