1

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

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

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

4

1 回答 1

2

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

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

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

于 2013-04-28T12:38:01.547 回答