0

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?

4
  • Why do you have that 'while' loop inside the 'if'? Commented May 13, 2014 at 18:35
  • I am reconsidering the if statement, but the while loop is so that it can prompt a dialog box telling the user the entered data was wrong and to show the number of tries left Commented May 13, 2014 at 18:37
  • I'm saying that I don't see why the "if" part exists. Commented May 13, 2014 at 18:38
  • Alright, I took the if statement off Commented May 13, 2014 at 18:40

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I need. I am trying to step through the code but I seem to be getting lost. When the user first presses the login button, the onClick is called. In on click it will get the user and pass and check them. If they are not correct, it will show you how many tries and then increase the counter, BUT how does it know to wait for the user to press the button again?
Because GUI's are event driven. wxPython waits for you to do "something", like click the button. Every time you click the button, it will run the event handler, check the username and password and show the dialog. I added the if statement back because it should only show the dialog if the username and password are incorrect. You will need to add an else condition to do something else if the credentials are correct though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.