Recently I have been trying to build a game in python without pygame
(I wanted a challenge) but have come across a problem. I can't use an if
loop in the mainloop.
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. I have also tried while
loops and for
loops but they don't work either.
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.
Inside this function you can perform any logic/code required to update your game. This is very similar to how pygame will have a function that re-draws/updates the items.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.