【问题标题】:Getting input form a GUI within a loop in python在python的循环中从GUI获取输入
【发布时间】:2014-05-13 18:27:39
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么在 'if' 中有那个 'while' 循环?
  • 我正在重新考虑 if 语句,但是 while 循环可以提示一个对话框,告诉用户输入的数据错误并显示剩余的尝试次数
  • 我是说我不明白为什么“如果”部分存在。
  • 好吧,我把 if 语句去掉了

标签: python wxpython wxwidgets


【解决方案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()

【讨论】:

  • 这正是我所需要的。我正在尝试单步执行代码,但我似乎迷路了。当用户第一次按下登录按钮时,会调用 onClick。在单击时,它将获取用户并通过并检查它们。如果它们不正确,它会显示尝试次数然后增加计数器,但是它如何知道等待用户再次按下按钮?
  • 因为 GUI 是事件驱动的。 wxPython 等待你做“某事”,比如点击按钮。每次单击按钮时,它都会运行事件处理程序,检查用户名和密码并显示对话框。我添加了 if 语句,因为它应该只在用户名和密码不正确时才显示对话框。如果凭据正确,您将需要添加 else 条件来执行其他操作。
  • 谢谢,我现在明白了。
猜你喜欢
  • 1970-01-01
  • 2013-02-10
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2015-04-20
  • 2016-02-22
  • 1970-01-01
相关资源
最近更新 更多