当消息框出现时,不要重置字段

时间:2013-05-07 04:10:47

标签: python wxpython

大家好我正在学习python语言,对于gui我使用wxpython。我的GUI中有2个旋转ctrl,1个文本ctrl,1个密码ctrl。每当用户输入错误密码时,都会弹出“密码错误”的消息。然后用户再次输入密码,但是发生的是密码字段用“2旋转ctrl和1文本ctrl”重置,我想保留spinctrl和textctrl的值,当用户输入错误的密码&消息框将显示。怎么实现这个?请建议我。

  class child(wx.Frame):    
  self.sc1 = wx.SpinCtrl(self,-1, pos=(-1, -1))
  self.paswd = wxTextCtrl(self, -1, (-1, -1),(30,20))
  def main(self, event):
  lan1 = self.sc1.GetValue()
  password = self.paswd.GetValue()
  if lan1 > 16 :
     wx.MessageBox("zzzzzzzzzzzz","warning")
     if password = license(hard coded):
        """show message box 
        and install"""
     else:
        """re enter password"""
        Child().Show()
   //when child window again pop up spin ctrl also set to 0, i want to keep spin ctrl value when user enter wrong password.     

1 个答案:

答案 0 :(得分:0)

您应该发布已有的内容,以便我们可以看到您出错的地方。知道你正在使用哪个版本的wxPython和Python以及哪个操作系统也是一件好事。无论如何,这是一种方法:

import wx

########################################################################
class LoginPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        userSizer = wx.BoxSizer(wx.HORIZONTAL)
        pwdSizer = wx.BoxSizer(wx.HORIZONTAL)

        userLbl = wx.StaticText(self, label="Username:")
        userSizer.Add(userLbl, 0, wx.CENTER|wx.ALL, 5)
        self.userTxt = wx.TextCtrl(self)
        userSizer.Add(self.userTxt, 1, wx.EXPAND|wx.ALL, 5)

        pwdLbl = wx.StaticText(self, label="Password:")
        pwdSizer.Add(pwdLbl, 0, wx.CENTER|wx.ALL, 5)
        self.pwdTxt = wx.TextCtrl(self, style=wx.TE_PASSWORD)
        pwdSizer.Add(self.pwdTxt, 1, wx.EXPAND|wx.ALL, 5)

        loginBtn = wx.Button(self, label="Login") 
        loginBtn.Bind(wx.EVT_BUTTON, self.onLogin)

        self.mainSizer.Add(userSizer, 0, wx.EXPAND)
        self.mainSizer.Add(pwdSizer, 0, wx.EXPAND)
        self.mainSizer.Add(loginBtn, 0, wx.CENTER|wx.ALL, 5)
        self.SetSizer(self.mainSizer)

    #----------------------------------------------------------------------
    def onLogin(self, event):
        """
        Login or show msg box if password wrong
        """
        username = self.userTxt.GetValue()
        pwd = self.pwdTxt.GetValue()

        if pwd == "password":
            print "logging in"
        else:
            msg = "Wrong password or username!"
            wx.MessageBox(msg, "Error", wx.OK|wx.ICON_INFORMATION)


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

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Login")
        panel = LoginPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = LoginFrame()
    app.MainLoop()