user2883071
user2883071

Reputation: 980

Getting input form a GUI within a loop in python

I have a text box which takes in user input and checks to see if the input is correct. If the input is not correct, I made a dialog box to ask the user to enter the information again with a count stating the number of tries left. However the dialog box keeps counting down and does not allow the user to enter any data.

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

how can I make it so the loop pauses until the user re-enters the user and password, then continues again?

Upvotes: 0

Views: 1048

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Your loop just keeps creating the message dialog over and over instead of letting the user do something. You need to remove the loop and put the counter outside of the event handler. Here's a runnable example:

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

Upvotes: 1

Related Questions