如何从函数中刷新变量的值,以便if语句能够识别它?

2024-10-03 13:25:51 发布

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

我正在尝试编程一个游戏,当用户按下按钮时,它会改变屏幕。我想使用'count'变量触发一个if语句,该语句将删除页面上的所有内容并创建下一个屏幕,但我的if语句在按下按钮后将无法识别'count'值的更改。我使用tkinter是因为这是我唯一熟悉的模块,我对编码非常陌生。我也尝试过使用while循环而不是if语句,但是没有成功。我使用if语句的目的是,不必在第一个按钮的函数中对其余的内容进行编程。我希望它的代码有点线性,以便它可以很容易地管理自己。如果有人知道如何刷新语句的变量,或者使用其他类型的语句,那么这将非常有用。我目前正在这个程序中使用这些模块:tkinter、time和os。谢谢你

count=0

#ALLOWS THE CONDITION TO BE MET TO MOVE SCREENS
def addCount():
    global count
    count+=1
    print(count)

#EXITS PROGRAM
def exitpls(*exitB):
    root.destroy()

#CREATE WINDOW
root = Tk()
root.title(";)")
root.config(bg="white")

#CREATE FRAME
frame = Frame(root, bg="white")
frame.pack(anchor=CENTER, side=BOTTOM, pady=15, expand=1)

#SIZE WINDOW
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
#window.overrideredirect(1) #ENABLE TO GO FULLSCREEN
root.geometry("%dx%d+0+0" % (w, h))
root.bind("<Escape>", exitpls)

# !FIRST SCREEN! Intro
introImagePath = "us.gif"
introImage = PhotoImage(file=introImagePath)
introImageLabel = Label(frame, image=introImage)
introImageLabel.grid(row=0, column=0, sticky=N, pady=10)

introLabel = Label(frame, text=introMessage, font="none 25", width=54, height=6, relief="sunken")
introLabel.grid(row=1, column=0, sticky=N, pady=10)

introButton = Button(frame, width=32, command=addCount, text="R  E  A  D  Y", font="none 40 bold", relief="raised")
introButton.grid(row=2, column=0, sticky=S, pady=10)

while count == 1:
    introImageLabel.grid_forget()
    introLabel.grid_forget()
    introButton.grid_forget()
    break

root.mainloop()

Tags: toifcountcolumnroot语句按钮frame