[英]I cant get an if loop to work in a tkinter code
最近我一直在嘗試在沒有pygame
的情況下在 python 中構建一個游戲(我想要一個挑戰),但遇到了一個問題。 我不能在主循環中使用if
循環。
這是我現在擁有的代碼,如果有人可以幫助我,我將不勝感激!
我試圖清除 canvas 然后在其中添加更多框,但if
循環再次不起作用。 我也嘗試過while
循環和for
循環,但它們也不起作用。
import tkinter as tk
import time
dieplease= "dieplease"
play = False
accepted = ["admin1 LET ME IN"] #accepted names
count = len(accepted)
score = 0
windowEA= tk.Tk()
boxy = tk.Canvas(windowEA, width = 400, height = 300)
boxy.pack() #canvas adjustments
label1 = tk.Label(windowEA, text="""You need Authorisation to get into this file so, please enter
your username and password to be allowed to play.""")
label1.config(font=('helvetica', 12)) #tkinter canvas config
boxy.create_window(200, 25, window=label1)
entry1 = tk.Entry (windowEA) #creation of username entry box
boxy.create_window(200, 100, window=entry1)
entry2 = tk.Entry (windowEA) #creation of password entry box
boxy.create_window(200, 120, window=entry2)
entry2.config(show="*")
x1 = entry1.get()
x2 = entry2.get()
namer = x1+" "+x2
def namecheck (): #checking if the name given fits with the allowed ones
global play
found = False
x1 = entry1.get()
x2 = entry2.get()
namer = x1+" "+x2
for d in range(count):
if namer == accepted[d]:
found = True
if found == True:
label1 = tk.Label(windowEA, text= "welcome to the game")
boxy.create_window(200, 230, window=label1)
output = "welcome to the game"
play = True
else:
label1 = tk.Label(windowEA, text= "Sorry incorrect information")
boxy.create_window(200, 230, window=label1)
output = "Sorry incorrect information"
def quit():
boxy.delete("all")
button1 = tk.Button(text="check", command=namecheck)
boxy.create_window(220, 180, window=button1) #checking if info is good button
button2 = tk.Button(text="Login", command=quit)
boxy.create_window(180, 180, window=button2) #login on button
if play == True:
print("HI") #checking
#this is where I want it to work
windowEA.mainloop()
利用 tkinter 中的 .after 方法來安排定期調用 function,比如每 100 毫秒。
在此 function 中,您可以執行更新游戲所需的任何邏輯/代碼。 這與 pygame 將有一個重新繪制/更新項目的 function 非常相似。
import tkinter as tk
def update():
#Code here to perform your if and any other code
print("Updating")
root.after(100,update)
root = tk.Tk()
root.after(100,update)
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.