我正在为学校做一个口袋妖怪游戏。首先,我有一个名为“登录”的开始游戏屏幕,用户必须在此屏幕上按下按钮,按下该按钮将触发一个新的窗口菜单,并关闭旧的登录屏幕。这种情况会发生,但在play()函数的主代码中,当我尝试向新屏幕菜单添加图像标签时,它会说菜单未定义。如果有人能帮忙,我将不胜感激。我的意图是不使用课堂,因为到目前为止我们还没有在课堂上学习到这一点
from tkinter import *
from tkinter import messagebox
login = Tk()
login.title("Pokemon")
login.geometry('1000x750')
login.resizable(False, False)
background_image=PhotoImage(file='background.png')
background_label = Label(login, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
title = PhotoImage(file='title.png')
lbl = Label(login, image = title)
lbl.place(x=25, y=20)
messagebox.showinfo('WELCOME TO MY POKEMON GAME', 'HOPE YOU HAVE A GREAT TIME :)')
def play():
global menu
global login
menu = Tk()
menu.title("Pokemon Main Menu")
menu.geometry('1000x750')
menu.resizable(False, False)
login.destroy()
begin= PhotoImage(file='begin.png')
btn = Button(login, image=begin, command = play)
btn.place(x=75, y=300)
background_image2=PhotoImage(file='background2.png')
background_label2 = Label(menu, image=background_image2)
background_label2.place(x=0, y=0, relwidth=1, relheight=1)
menu.mainloop()
login.mainloop()
这是我一直收到的错误
Traceback (most recent call last):
File "/Users/ishaan/Desktop/attachments/Pokemon.py", line 39, in <module>
background_label2 = Label(menu, image=background_image2)
NameError: name 'menu' is not defined
你的问题是错误的缩进
您必须在
play()
内部创建标签,但在play()
外部创建标签,所以它在开始时执行,而不是在您按下按钮时执行,所以它会在创建menu
之前尝试添加标签相关问题 更多 >
编程相关推荐