gpt4 book ai didi

python - 在 Python 中使用 Tkinter 时程序挂起

转载 作者:行者123 更新时间:2023-12-01 05:45:32 25 4
gpt4 key购买 nike

我正在制作一个Python程序来跟踪各种用户的优点和排名。它需要有一个图形用户界面。但是,当我添加 while 循环时,它挂起!需要 while 循环来保持程序直到给出输入。这是代码:

def __init__(self):
global master, mainCanvas;
tree.write('./oldUsrData.xml')
god = self
#Create Base Window
master=Tk()
master.title("Briar Woods Falcon Robotics Merit Tracker 2.0")
master.maxsize(500,500)

#Create the Credit Label
creditLabel = Label(master, text="Developed by Falcon Robotics. Powered by Python.")
creditLabel.grid(row = 1, column= 1)
creditLabel.pack()

#Make the Main Canvas
mainCanvas = Canvas(master, width = 500, height=500, fill = None)

#Password Entry
inputPass = StringVar()
passwordEntry = Entry(master, textvariable=inputPass, show="$")
passwordEntry.grid(row=2, column=1)

#Define a few Action Functions
def startSetUp():
god.setUp()
def checkPassword(self):
if inputPass.get() == encryptionKey:
passwordEntry.destroy()
mainCanvas.create_text(250,250,text="CORRECT PASSWORD", tags="correctPassword")
continueButton = Button(master, text="Continue", command=startSetUp)
mainCanvas.create_window(270,270, window=continueButton, tags="correctPassword")
else:
exit()

passwordEntry.bind('<Key-Return>', checkPassword)
passwordEntry.pack()
mainCanvas.pack()
master.mainloop()

#define the merit ranks
global meritDict;
meritDict = { -4: 'Untouchable',
-3: 'Scum',
-2: 'Criminal',
-1: 'Mindless Grunt',
0: 'Citizen',
1: 'Vigilante',
2: 'Generic Hero',
3: 'Sharkboy/ Lavagirl',
4: 'Wonderwomen/Matter-eating lad',
5: 'Member of the Justice League',
6: 'X-men',
7: 'Avenger'}
def setUp(self):
#Verify Merit Dictionary
mainCanvas.delete("correctPassword")
mainCanvas.create_text(30,30,text="This is the Merit Ranking System. Change Program Source Code to edit",anchor="nw", tags="merit")
for x in range(-4,8,1):
mainCanvas.create_text(200,(x+4)*20+50, text= str(x) + ": " + str(meritDict[x]), anchor='w')

#create Quitter function
quitted = False
def quitter():
quitted = True
exit()
quit()
quitterButton = Button(master, text="Quit", command=quitter)
mainCanvas.create_window(50, 330, window=quitterButton, tag="quitter")

#Create User Name Entry
userEntryFinished = False;
def getUserEntry():
userVar = StringVar()
user = ""
def userEnter(self):
user = userVar.get()
mainCanvas.create_text(250, 350, text="User Inputted: " + user, tags="userEnter");
userEntryFinished=True;
userEntry = Entry(master, textvariable=userVar)
mainCanvas.create_window(250, 330, window=userEntry, tags="userEnter")
userEntry.bind('<Key-Return>', userEnter)

getUserEntry();
while not userEntryFinished:
pass
... #<--Further, irrelevant code

代码继续,但通过反复试验,我确定 while 循环是错误的根源。另外,我需要接受输入,直到按下退出按钮为止,那么我该怎么做呢?另外,为什么所有 while 循环都会导致这个奇怪的问题?我正在使用 tkinter 和 python 2.6。

注意:一切都已定义,只是未包含在这段代码中。树和根是全局的。

澄清:按下“继续”按钮时代码挂起另外:有没有办法只等待用户输入?那会有很大帮助。

最佳答案

您的代码已经有一个“while 循环”——即您调用 mainloop 时创建的循环。在 GUI 编程中,您不应该在代码中创建自己的循环来等待用户输入。相反,您创建小部件,然后响应这些小部件中/上发生的事件。

程序挂起的具体原因是 while 循环阻止事件循环执行应该执行的操作,即响应事件。不仅仅是用户事件,还有系统重绘自身的请求。

解决方案只是删除 while not userEntryFinished 循环,而是重新设计代码以响应事件。将循环之后的所有代码放入函数中。然后,在getUserEntry中,除了设置标志之外,还可以调用这个函数,而不是/。

关于python - 在 Python 中使用 Tkinter 时程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com