在python的循环中从GUI获取输入

时间:2014-05-13 18:27:40

标签: python wxpython wxwidgets

我有一个文本框,它接收用户输入并检查输入是否正确。 如果输入不正确,我做了一个对话框,要求用户再次输入信息,并显示一个计数,说明剩余的尝试次数。但是,对话框不断倒计时,不允许用户输入任何数据。

def OnClick2(self,event):
        password=self.enteredPass.GetValue() #takes the password form the textbox
        user=self.enteredUser.GetValue() #takes the username form the textbox
        count=0 # count for the number of tries
        while (user!="Username" and password!="Password"): #loops untill it is right
            dlg=wx.MessageDialog(self,"You have %s tries left"%(str(3-count)),"",wx.OK)
            dlg.ShowModal()
            dlg.Destroy()
            count=count+1
            password=self.enteredPass.GetValue() #retakes the password
            user=self.enteredUser.GetValue() #retakes the username
            if (count==3):
                self.Destroy()
                break

我该怎么做才能让循环暂停,直到用户重新输入用户和密码,然后再继续?

1 个答案:

答案 0 :(得分:1)

你的循环只是反复创建消息对话框而不是让用户做某事。您需要删除循环并将计数器放在事件处理程序之外。这是一个可运行的例子:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = wx.Panel(self)
        self.count = 0

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        usernameLbl = wx.StaticText(panel, label="Username:")
        self.username = wx.TextCtrl(panel)
        self.addWidgets(usernameLbl, self.username)

        pwLbl = wx.StaticText(panel, label="Password:")
        self.pw = wx.TextCtrl(panel)
        self.addWidgets(pwLbl, self.pw)

        btn = wx.Button(panel, label="Login")
        btn.Bind(wx.EVT_BUTTON, self.onClick)
        self.mainSizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)

        panel.SetSizer(self.mainSizer)
        self.Show()

    #----------------------------------------------------------------------
    def addWidgets(self, lbl, txt):
        """"""
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(txt, 1, wx.ALL|wx.EXPAND, 5)
        self.mainSizer.Add(sizer, 0, wx.ALL|wx.EXPAND)

    #----------------------------------------------------------------------
    def onClick(self, event):
        """"""
        password = self.pw.GetValue()
        user = self.username.GetValue()

        if user!="Username" and password!="Password":
            # count for the number of tries 
            msg = "You have %s tries left"%(str(3-self.count))
            dlg = wx.MessageDialog(self, msg, "", wx.OK)
            dlg.ShowModal()
            dlg.Destroy()

            self.count += 1
            password=self.pw.GetValue() #retakes the password
            user=self.username.GetValue() #retakes the username
            if self.count == 3:
                self.Destroy()


#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()