Python 和 tkinter:全局变量不起作用?

发布于 2025-01-11 14:39:43 字数 1104 浏览 0 评论 0原文

这是一个使用 tkinter 的简单程序。它基本上应该显示用户在 inp 字段中键入的任何内容,并在单击按钮时将其显示为标签。 在这里,我尝试将 tkinter 值包含为全局变量,然后在 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()

但是在运行时,单击按钮会导致显示空标签。

但是,如果我在change_label() 中声明new_text,则代码可以正常工作。

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()

为什么第一个代码不起作用而第二个代码却起作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡莣 2025-01-18 14:39:43

首先,你不应该在函数中声明一个全局变量,除非它至少被调用一次,否则它不会是全局的

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():
    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()
岁吢 2025-01-18 14:39:43

首先,你不应该在函数中声明一个全局变量,除非它至少被调用一次,否则它不会是全局的

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()

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()
庆幸我还是我 2025-01-18 14:39:43

使用 StringVar 可以帮助您比 Global 更有效地管理 LabelEntry 等小部件的值。我在 Entry 小部件中添加了 textvariable 。另外,重新排列代码以提高可读性

第一个代码:

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()

单击前的结果:

在此处输入图像描述

单击后的结果:

在此处输入图像描述

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:

enter image description here

Result after clicking:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文