【发布时间】:2013-04-21 23:10:38
【问题描述】:
我正在制作一个 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 一起使用。
注意:所有内容都已定义,只是未包含在此 sn-p 代码中。树和根是全局的。
澄清:按下“继续”按钮时代码挂起 另外:有没有办法只等待用户输入?那会有很大帮助。
【问题讨论】:
-
startSetUp在哪里定义?而(self=root)在setUp中的作用是什么? -
startSetUp定义在checkPassword的正上方,而self=root只是前一个命令行版本的剩余部分。现在不用了,我已经删除了。 -
你不应该在函数内部定义太多的函数,因为你几乎在任何地方都混合了变量和函数的范围。例如,
passwordEntry.bind('<Key-Return>', checkPassword)应该是passwordEntry.bind('<Key-Return>', self.checkPassword)。 -
感谢您的建议。我对 tkinter(以及一般的 GUI 编程)很陌生,所以我只是把东西混合起来看看有什么用。
标签: python input while-loop tkinter