article

Saturday, May 28, 2016

wxpython widgets wx.Button

wxpython widgets wx.Button

wx.Button is a simple widget. It contains a text string. It is used to trigger an action.

 
import wx


class Example(wx.Frame):
           
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw) 
        
        self.InitUI()
        
    def InitUI(self):   

        pnl = wx.Panel(self)
        cbtn = wx.Button(pnl, label='Close', pos=(20, 30))#pos position
        #The wx.EVT_BUTTON event is triggered when we click on the button. We specify the event handler for the event.
        cbtn.Bind(wx.EVT_BUTTON, self.OnClose) #call function def OnClose(self, e):

        self.SetSize((250, 200)) #set form size
        self.SetTitle('wx.Button') #set form title
        self.Centre() #center form
        self.Show(True) #show form         
        
    def OnClose(self, e):
        
        self.Close(True) #close form                  
        
def main():
    
    ex = wx.App()
    Example(None) #call frame class Example(wx.Frame):
    ex.MainLoop()    


if __name__ == '__main__':
    main()   

wxpython widgets wx.Button when click change label value
 
import wx


class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent=parent)
        self.panel = wx.Panel(self)
        self.label = {}
        for i in range(5):
            button = wx.Button(
                self.panel, -1, label='b' + str(i), pos=(20, 30 * i))
            button.Bind(wx.EVT_BUTTON, self.on_button)
            label = wx.StaticText(self.panel, -1, label='label' + str(
                i), pos=(120, 30 * i), name='label' + str(i))
            self.label[button] = label

    def on_button(self, event):
        button = event.GetEventObject()
        label = self.label[button]
        label.SetLabel('sss')

x = wx.App()
y = MyFrame(None).Show()
x.MainLoop()

Related Post