使用Python在循环中从GUI中获取输入

2024-10-03 21:27:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

如何使循环暂停,直到用户重新输入用户和密码,然后再继续?在


Tags: the用户selfformcountusernamepassword对话框
1条回答
网友
1楼 · 发布于 2024-10-03 21:27:53

循环只是不断地创建消息对话框,而不是让用户做一些事情。您需要移除循环并将计数器放在事件处理程序之外。下面是一个可运行的示例:

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()

相关问题 更多 >