程序在Python中使用Tkinter时挂起

时间:2013-04-28 02:57:16

标签: python input while-loop tkinter

我正在制作一个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。

注意:所有内容都已定义,但未包含在此代码段中。树和根是全球性的。

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

1 个答案:

答案 0 :(得分:2)

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

程序挂起的具体原因是因为你的while循环阻止了事件循环做了应该做的事情,即响应事件。不仅是用户事件,还有来自系统重绘自身的请求。

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