简体   繁体   English

我无法让 if 循环在 tkinter 代码中工作

[英]I cant get an if loop to work in a tkinter code

Recently I have been trying to build a game in python without pygame (I wanted a challenge) but have come across a problem.最近我一直在尝试在没有pygame的情况下在 python 中构建一个游戏(我想要一个挑战),但遇到了一个问题。 I can't use an if loop in the mainloop.我不能在主循环中使用if循环。

This is the code I have now and I would appreciate if someone could give me a helping hand!这是我现在拥有的代码,如果有人可以帮助我,我将不胜感激!

I have tried to clear the canvas and then add more boxes in but again the if loop won't work.我试图清除 canvas 然后在其中添加更多框,但if循环再次不起作用。 I have also tried while loops and for loops but they don't work either.我也尝试过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()

Make use of the.after method in tkinter to schedule a function to be called periodically, say every 100ms.利用 tkinter 中的 .after 方法来安排定期调用 function,比如每 100 毫秒。

Inside this function you can perform any logic/code required to update your game.在此 function 中,您可以执行更新游戏所需的任何逻辑/代码。 This is very similar to how pygame will have a function that re-draws/updates the items.这与 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM