按钮1未定义?

2024-06-16 09:56:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

import sys
from tkinter import *


#first new screen
def hypoténusegetdef ():
    widgets1 = button1
    nextscreen1(widgets1)

def next_screen1(names):
    for widget in names:
        widget.place_forget() 
        hyplabel1 = Label (text = "This is my text")



def next_screen(names):
    for widget in names:
        widget.place_forget()
        button1 = Button (text = "Button1",fg = "blue",command = hypoténusegetdef)
        button1.grid (row = 1,column = 2)


def forget_page1():
    widgets = [mLabel1, button]
    next_screen(widgets)

################################################################################

#first page things
mGui = Tk ()

mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
                 fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)

button = Button (text = "Next", command = forget_page1 )
button.place(x = 275,y = 230)

mGui.mainloop()

我希望用户单击“下一步”,然后一个按钮显示为“button1”,单击该按钮后,文本应显示“这是我的文本”,但它给我一个错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Python33\Projects\MyMathDictionary.py", line 7, in hypoténusegetdef
widgets1 = button1
NameError: global name 'button1' is not defined

任何帮助都将受到感激:


Tags: textinnamesdefplacewidgetscreennext
1条回答
网友
1楼 · 发布于 2024-06-16 09:56:03

button1在下一个屏幕中定义,但在hypopénusegetdef中使用,不能使用一个函数中的变量。看看其余的代码,最简单的方法可能是使用一个可以在任何地方访问的全局变量(通常是不好的做法,但对于简短的脚本,它们可以让事情变得更简单)

相关问题 更多 >