按住一个面板并显示另一个面板后,指向另一个功能的按钮

时间:2012-03-05 12:45:57

标签: python wxpython

我有这种情况。

我的动机:隐藏一个面板并展示另一个面板。

什么工作:布局明智,我的应用程序表现得像我想要的那样。

问题:在我隐藏一个面板并显示另一个面板后,此面板中的按钮(处理按钮)仍然指向使用“OUT”按钮绑定的功能。

要遵循的步骤

  1. 运行程序。
  2. 按“处理”按钮。然后按“Out”按钮。
  3. 你会看到旧面板回来。再次按“处理”按钮。
  4. 注意按钮正在运行与“OUT”按钮绑定的功能。
  5. 这是完整的程序:

    import wx
    
    
    class Panels(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title,size=(500,-1))
    
                    #Font
            #__init__(self, pointSize, family, style, weight, underline=False, face=EmptyString, encoding=FONTENCODING_DEFAULT) 
            self.font1 = wx.Font(12, wx.DECORATIVE, wx.ITALIC,wx.FONTWEIGHT_BOLD, underline = True )
            self.font2 = wx.Font(10, wx.DECORATIVE, wx.NORMAL,wx.FONTWEIGHT_BOLD, underline = False )
            self.font3 = wx.Font(10, wx.DECORATIVE, wx.ITALIC,wx.FONTWEIGHT_BOLD, underline = True )
    
            favicon = wx.Icon('icon.ico', wx.BITMAP_TYPE_ICO)
            wx.Frame.SetIcon(self, favicon)
    
            list_choice = ['ALL','UnSeen','DRAFT']
    
            self.vbox = wx.BoxSizer(wx.VERTICAL)
    
    #Launch panel Defined
    
            panel_launch = wx.Panel(self,-1,style=wx.SUNKEN_BORDER)
    
    
    
    #Layout Defination
    
            hbox= wx.BoxSizer(wx.HORIZONTAL)
            lbl_Username = wx.StaticText(panel_launch,0, "Username:")
            lbl_Username.SetFont(self.font3) 
            self.txt_Username = wx.TextCtrl(panel_launch,0)#wx.Size(self,w,h)
            lbl_Password = wx.StaticText(panel_launch,0, "Password:")
            lbl_Password.SetFont(self.font3) 
            self.txt_Password = wx.TextCtrl(panel_launch,0,style=wx.TE_PASSWORD)
    
            btn_Process = wx.Button(panel_launch,0, "Process")
    
            hbox.Add(lbl_Username,0,wx.LEFT|wx.TOP,10)
            hbox.Add(self.txt_Username,0,wx.LEFT|wx.TOP,10)
            hbox.Add(lbl_Password,0,wx.LEFT|wx.TOP,10)
            hbox.Add(self.txt_Password,0,wx.LEFT|wx.TOP,10)
            hbox.Add(btn_Process,0,wx.LEFT|wx.TOP,10)
            panel_launch.SetSizer(hbox)
    
    
    #Menu section
            menubar=wx.MenuBar()
            pehla=wx.Menu()
            doosra=wx.Menu()
            teesra =wx.Menu()
            option_menu=wx.Menu()
            info=wx.Menu()
    
    #Menu Items
    
            self.item1_1=pehla.Append(wx.ID_OPEN,"&Add Mail\tAlt-A","This is to add a mail account") #Sub-Items of First menu pull down list
            item1_2=pehla.Append(wx.ID_EXIT,"&Quit\tAlt-Q","This will exit app") #The last comment will show on status bar when mouse is on that option
            item3_2=teesra.Append(wx.ID_ABOUT,"A&bout\tAlt-B","About Section")
    
    
            menu_1=menubar.Append(pehla,'&File')    #Naming of Menu items
            menu_2=menubar.Append(doosra,'&Edit')
            menu_3=menubar.Append(teesra,'&Info')
            item2_1=option_menu.Append(wx.ID_ANY,'Export File Location')
            doosra.AppendMenu(wx.ID_ANY,"&Options\tAlt-O",option_menu)
            self.item1_1.Enable(False)
            self.SetMenuBar(menubar)
    
            self.vbox.Add(panel_launch,-1,wx.EXPAND,10)
    
            self.SetSizer(self.vbox)
    
            self.Layout()
    
            self.Centre()
    
            self.Show(True)
    
    #Event Bindings
    
            self.Bind(wx.EVT_BUTTON,self.OnSignIn,btn_Process)
            self.Bind(wx.EVT_MENU,self.OnAddMail,self.item1_1)
            self.Bind(wx.EVT_MENU, self.OnFileExit,item1_2)
    
    
    
    #+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-
    #--------------------------Frame Related Function Zone--------------------------------------
    
        def OnSignIn(self,event):
            print "We are in default"
    
            mymail = self.txt_Username.GetValue()
            mypwd = self.txt_Password.GetValue()
            self.tstremove(event) #Hiding the old panel items
            panel_signedin = wx.Panel(self,-1)
            lbl_email = wx.StaticText(panel_signedin,0, "Email:")
            lbl_email.SetFont(self.font3) 
            txt_mymail = wx.StaticText(panel_signedin,0,mymail)
            txt_mymail.SetFont(self.font2)
            btn_OUT = wx.Button(panel_signedin,0, "OUT")
            self.Bind(wx.EVT_BUTTON,self.OnSignOut,btn_OUT)
            hbox3= wx.BoxSizer(wx.HORIZONTAL)
    
            hbox3.Add(lbl_email,0,wx.ALL,10)
            hbox3.Add(txt_mymail,0,wx.ALL,10)
            hbox3.Add(btn_OUT,0,wx.ALL,10)
            panel_signedin.SetSizer(hbox3)
            self.vbox.Add(panel_signedin,-1,wx.EXPAND,10)
            self.SetSizer(self.vbox)
            self.Layout()
            self.item1_1.Enable(True) #Enable 'Add' menu item
    
        def OnSignOut(self,event):
    
            print "We are out"
            event.GetEventObject().GetParent().Hide()
    
            self.txt_Username.GetParent().Show(True)
            self.Layout()
            return
    
    
        def OnSignInNew(self,event):
    
            print "We are in New"
    
            self.item1_1.Enable(True)
            mymail1 = self.txt1_Username.GetValue()
            mypwd1 = self.txt_Password.GetValue()
            self.tstremove(event)
            panel_signedin1 = wx.Panel(self,-1)
            lbl_email1 = wx.StaticText(panel_signedin1,0, "Email:")
            lbl_email1.SetFont(self.font3) 
            txt_mymail1 = wx.StaticText(panel_signedin1,0,mymail1)
            txt_mymail1.SetFont(self.font2)
            btn1_OUT = wx.Button(panel_signedin1,0, "OUT")
            self.Bind(wx.EVT_BUTTON,self.OnSignOut,btn1_OUT)
    
            hbox31= wx.BoxSizer(wx.HORIZONTAL)
            hbox31.Add(lbl_email1,0,wx.ALL,10)
            hbox31.Add(txt_mymail1,0,wx.ALL,10)
            hbox31.Add(btn1_OUT,0,wx.ALL,10)
            panel_signedin1.SetSizer(hbox31)
            self.vbox.Add(panel_signedin1,-1,wx.EXPAND,10)
            self.SetSizer(self.vbox)
            self.Layout()
    
    
        def tstremove(self,event):
    
            event.GetEventObject().GetParent().Hide()
            self.Layout()
            return
    
        def tstrecall(self,event):
            event.GetEventObject().GetParent().UnHide()
            self.Layout()
            return
    
    
        def OnAddMail(self,eventdef):
            self.item1_1.Enable(False)
            self.vbox.Add((-1,5))        
            panel_new = wx.Panel(self,0,style=wx.SUNKEN_BORDER)
            hbox1= wx.BoxSizer(wx.HORIZONTAL)
            lbl1_Username = wx.StaticText(panel_new,-1, "Username:")
            lbl1_Username.SetFont(self.font3) 
            self.txt1_Username = wx.TextCtrl(panel_new,-1)#wx.Size(self,w,h)
            lbl1_Password = wx.StaticText(panel_new,-1, "Password:")
            lbl1_Password.SetFont(self.font3) 
            self.txt1_Password = wx.TextCtrl(panel_new,-1,style=wx.TE_PASSWORD)
    
            btn1_Process = wx.Button(panel_new,-1, "Process")
            self.Bind(wx.EVT_BUTTON,self.OnSignInNew,btn1_Process)
    
            hbox1.Add(lbl1_Username,0,wx.LEFT|wx.TOP,10)
            hbox1.Add(self.txt1_Username,0,wx.LEFT|wx.TOP,10)
            hbox1.Add(lbl1_Password,0,wx.LEFT|wx.TOP,10)
            hbox1.Add(self.txt1_Password,0,wx.LEFT|wx.TOP,10)
            hbox1.Add(btn1_Process,0,wx.LEFT|wx.TOP,10)
            panel_new.SetSizer(hbox1)
    
            self.vbox.Add(panel_new,-1,wx.EXPAND,10)
            self.SetSizer(self.vbox)
            self.Layout()
            self.Bind(wx.EVT_BUTTON,self.OnSignInNew,btn1_Process)
            return
    
    #+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-+-()-
    #--------------------------Process Related Function Zone--------------------------------------
    
        def OnFileExit(self, event):
            """ File|Exit event """
            self.Close()
    
    
    
    
    app = wx.App()
    Panels(None, -1, 'Test')
    app.MainLoop()
    

    我只是无法得到,一个按钮如何指向另一个功能? 隐藏和显示不够?

1 个答案:

答案 0 :(得分:2)

问题是你给了两个相同ID的BOTH按钮。实际上,你几乎给所有小部件提供相同的ID。你永远不应该这样做。如果您将所有小部件中的id参数更改为wx.ID_ANY或仅-1,您将获得随机分配的ID,您的程序将起作用。