Python 我的tkinter代码因按钮或输入框而崩溃
最近我想开始学习tkinter,但我的代码有问题。当我运行它,我可以登录,但在我登录后,它崩溃。我不知道为什么会发生这种情况,并尝试了一些事情。 我已经设法使文本出现(重新排列),所以我确信它是按钮或输入框Python 我的tkinter代码因按钮或输入框而崩溃,python,tkinter,Python,Tkinter,最近我想开始学习tkinter,但我的代码有问题。当我运行它,我可以登录,但在我登录后,它崩溃。我不知道为什么会发生这种情况,并尝试了一些事情。 我已经设法使文本出现(重新排列),所以我确信它是按钮或输入框 import random import time import tkinter as tk global play global boxy run = False killme = "" scorefile = open("Scores.txt", "w") songfile = op
import random import time import tkinter as tk
global play global boxy
run = False killme = "" scorefile = open("Scores.txt", "w") songfile = open("Songs.txt", "r") scf = scorefile sf = songfile.readlines() count
= len(sf) output = (count/4)-1 for f in range(0,count):
sf[f] = sf[f].rstrip("\n")
x = 0 play = False found = False correct = False score = 0 song = "n/a" songs = ["1", "2" ,"3" ,"4" ,"5"] y = 0 randn = 0 point = 0
windowEA= tk.Tk()
boxy = tk.Canvas(windowEA, width = 500, height = 500) boxy.pack()
#canvas ajustments
#-------------------------------------Login-------------------------------------
label1 = tk.Label(windowEA, text="""You need Authorisation to get into this file so, please enter your username and password to be premitet to play.""") label1.config(font=('helvetica', 12)) #tkinter canvas config boxy.create_window(250, 25, window=label1)
entry1 = tk.Entry (windowEA) #creation of username entry box boxy.create_window(250, 100, window=entry1) entry2 = tk.Entry (windowEA) #creation of password enrty box boxy.create_window(250, 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
passwordfile = open("Authorisation names.txt", "r")
if passwordfile.mode == "r" :
f1 = passwordfile.readlines()
for x in f1:
check = x
if check == namer:
found = True
if found == True:
label1 = tk.Label(windowEA, text= "welcome to the game")
boxy.create_window(250, 230, window=label1)
output = "welcome to the game"
play = True
boxy.delete(all)
else:
label1 = tk.Label(windowEA, text= "Sorry incorect information")
boxy.create_window(250, 230, window=label1)
output = "Sorry incorect information"
passwordfile.close()
button1 = tk.Button(text="Log In", command=namecheck) boxy.create_window(250, 180, window=button1) #checking if info is good button
#-------------------------------------Login-------------------------------------
#-------------------------------------output------------------------------------ def update():
global play
gay = False
if play == True:
gay = True
t = 0
print("good update")
while gay == True:
print("good play")
def checkguess():
gu1 = entry69.get()
if gu1 == sf[rands]:
correct = True
point = point + 3
else:
gu1 = entry69.get()
if gu1 == sf[rands]:
correct = True
point = point + 1
if correct == True:
print("Weldone the answer was "+sf[rands]+". You now have "+str(point)+" points")
else:
print("Good try the answer was "+sf[rands]+". But you still have "+str(point)+" points")
label69 = tk.Label(windowEA, text=("""During this game you will be given the"""))
#first letter of every word from a song
#and the artists,you then have to work out what the song is called using the
#infomation, you have 2 guesses and will be told what it was after you finish.
#Have fun!(Capitals matter)(english letters only)."""))
label69.config(font=('helvetica', 15))
boxy.create_window(250, 230, window=label69)
rand = random.randint(0,10)
randn = int(rand)*2
rands = randn + 1
button69 = tk.Button(text="enter", command=checkguess)
boxy.create_window(250, 180, window=button69) #checking if info is good button
label420 = tk.Label(windowEA, text= sf[randn])
boxy.create_window(250, 100, window=label420)
entry69 = tk.Entry(windowEA) #creation of username entry box
boxy.create_window(250, 140, window=entry69)
# repeat = input("Do you want to play again?")
# if repeat == "No" or repeat == "no" or repeat == "NO" or repeat == "nO":
# gay = False
# songfile.close()
# save = input("Do you want to save your score? ")
# if save != "NO" or save != "No" or save != "no" or save != "nO":
# scorefile.write(namer+" = "+str(point)+" points" + "\n")
# scorefile.close()
# print(scf)
#print("leaderboard:")
# print(""" Thanks for playing
# I hope you will play again soon!""")
windowEA.after(100,update)
#-------------------------------------output------------------------------------
windowEA.after(10,update) windowEA.mainloop()
你能比“it崩溃”更具体一点吗?它是如何崩溃的?你有错误吗?如果是这样的话,错误是什么?当我运行代码并完成登录过程时,我按下登录按钮,然后它会按应有的方式清除所有内容,但是tkinter画布没有响应,因此我必须关闭它。它可能会变得无响应,因为您有一个无限循环。是否添加了调试语句以查看代码正在执行的操作?一旦你进入循环,
gay
就不可能变成False
。我可能会在周一(下次可以)检查这种情况,谢谢你的帮助