如果语句在Python3中无法正常工作
当分数大于或等于 5 时,我试图打印出一个字符串,但它不会打印。我在这里做错了什么?
from tkinter import *
top = Tk()
top.geometry("1000x720")
Score = 0
var = StringVar()
sline = Label(top, textvariable=var)
sline.place(x = 100, y = 395)
var.set("Score: ")
scorevar = StringVar()
label = Label(top, textvariable=scorevar)
label.place(x = 140, y = 395)
scorevar.set("0")
def getscore():
global Score
Score = Score + 1
scorevar.set(Score)
print(Score)
bclick = Button(top, fg='white', bg='RoyalBlue3', activebackground='azure', highlightcolor='azure', bd=4, padx=10, pady=5, text = "Click!", command = getscore)
bclick.place(x = 100,y = 350)
if Score >= 5:
print("Hey, you're rich now!")
top.mainloop()
回答
你需要把if
里面,getscore()
否则它只执行一次:
def getscore():
global Score
Score = Score + 1
scorevar.set(Score)
print(Score)
if Score >= 5:
print("Hey, you're rich now!")
有了这个变化,每次getscore()
函数执行时,都会执行,if
并且有机会print()
调用额外的。