Reputation: 355
I am making a python program to track various user merits and ranks. It needs to have a graphical user interface. However, when I add a while loop, it hangs! The while loop is needed to hold up the program until input is given. Here is the code:
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
The code continues, but through trial and error, I determined that the while loop was the source of error. Also, I will need to take input until the quit button is pressed, so how can I go about that? Also, why do all while loops cause this strange problem? I am using tkinter with python 2.6.
Note: Everything is already defined, just not included in this snippet of code. tree and root are global.
Clarification: Code Hangs when the "Continue" Button is pressed Also: Is there a way to just wait for user input? That would help a lot.
Upvotes: 1
Views: 1359
Reputation: 385950
Your code already has a "while loop" -- that is the loop created when you call mainloop
. In GUI programming, you shouldn't be creating your own loops in code to wait for user input. Instead, you create widgets and then respond to events that occur in/on those widgets.
The specific reason your program hangs is because your while loop prevents the event loop from doing what is supposed to do, which is to respond to events. Not just user events, but requests from the system to redraw itself.
The solution is simply to remove your while not userEntryFinished
loop, and instead redesign your code to respond to events. Put all the code that is after that loop into a function. Then, in getUserEntry
, instead of / in addition to setting the flag, you can call this function.
Upvotes: 2