如何在Wxpython中动态创建复选框或按钮?

如何在Wxpython中动态创建复选框或按钮?,python,python-2.7,wxpython,Python,Python 2.7,Wxpython,当我执行此操作时,会出现复选框,但它们不起作用,这意味着我无法选中任何复选框,动态添加复选框或按钮的正确方法是什么 我现在已经编辑了我的代码并将其添加到面板中,现在复选框甚至都不会出现 posx = 50 for name in sheets: wx.CheckBox(self, -1 ,name, (15, posx)) posx = posx + 20 复选框类-> 按钮类-> 复选框的示例代码: pnl = wx.Panel(self) posx = 50 for nam

当我执行此操作时,会出现复选框,但它们不起作用,这意味着我无法选中任何复选框,动态添加复选框或按钮的正确方法是什么

我现在已经编辑了我的代码并将其添加到面板中,现在复选框甚至都不会出现

posx = 50
for name in sheets:
    wx.CheckBox(self, -1 ,name, (15, posx))
    posx = posx + 20
  • 复选框类->

  • 按钮类->

  • 复选框的示例代码:

    pnl = wx.Panel(self)
    posx = 50
    for name in sheets:
        cb = wx.CheckBox(pnl, label=name, pos=(20, posx))
        cb.SetValue(True)
        cb.Bind(wx.EVT_CHECKBOX, self.doSomething)
        posx = posx + 20
    
    def doSomething(Self,e):
    sender = e.GetEventObject()
    isChecked = sender.GetValue()
    
    if isChecked:
        #do something here            
    else: 
        #do something else here       
    
  • 按钮的示例代码:

      #!/usr/bin/python
     # -*- coding: utf-8 -*-
    
      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)
    
       cb = wx.CheckBox(pnl, label='Show title', pos=(20, 20))
       cb.SetValue(True)
    
       cb.Bind(wx.EVT_CHECKBOX, self.ShowOrHideTitle)
    
       self.SetSize((250, 170))
       self.SetTitle('wx.CheckBox')
       self.Centre()
       self.Show(True)    
    
    def ShowOrHideTitle(self, e):
    
      sender = e.GetEventObject()
      isChecked = sender.GetValue()
    
      if isChecked:
        self.SetTitle('wx.CheckBox')            
      else: 
        self.SetTitle('')        
    
     def main():
    
     ex = wx.App()
      Example(None)
      ex.MainLoop()    
    
     if __name__ == '__main__':
     main()   
    
  • 这是所有其他wxpython小部件的好教程:(wx.Button) wx.ToggleButton、wx.StaticLine、wx.StaticText、wx.StaticBox wx.ComboBox、wx.CheckBox、wx.StatusBar、wx.RadioButton、wx.Gauge、wx.Slider和wx.SpinCtrl) ->

  • 复选框类->

  • 按钮类->

  • 复选框的示例代码:

    pnl = wx.Panel(self)
    posx = 50
    for name in sheets:
        cb = wx.CheckBox(pnl, label=name, pos=(20, posx))
        cb.SetValue(True)
        cb.Bind(wx.EVT_CHECKBOX, self.doSomething)
        posx = posx + 20
    
    def doSomething(Self,e):
    sender = e.GetEventObject()
    isChecked = sender.GetValue()
    
    if isChecked:
        #do something here            
    else: 
        #do something else here       
    
  • 按钮的示例代码:

      #!/usr/bin/python
     # -*- coding: utf-8 -*-
    
      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)
    
       cb = wx.CheckBox(pnl, label='Show title', pos=(20, 20))
       cb.SetValue(True)
    
       cb.Bind(wx.EVT_CHECKBOX, self.ShowOrHideTitle)
    
       self.SetSize((250, 170))
       self.SetTitle('wx.CheckBox')
       self.Centre()
       self.Show(True)    
    
    def ShowOrHideTitle(self, e):
    
      sender = e.GetEventObject()
      isChecked = sender.GetValue()
    
      if isChecked:
        self.SetTitle('wx.CheckBox')            
      else: 
        self.SetTitle('')        
    
     def main():
    
     ex = wx.App()
      Example(None)
      ex.MainLoop()    
    
     if __name__ == '__main__':
     main()   
    
  • 这是所有其他wxpython小部件的好教程:(wx.Button) wx.ToggleButton、wx.StaticLine、wx.StaticText、wx.StaticBox wx.ComboBox、wx.CheckBox、wx.StatusBar、wx.RadioButton、wx.Gauge、wx.Slider和wx.SpinCtrl) ->

  • 这很有效

    import wx
    
    class MyFrame(wx.Frame):
       """make a frame, inherits wx.Frame"""
    
    def __init__(self):
       # create a frame, no parent, default to wxID_ANY
    
     wx.Frame.__init__(self, None, wx.ID_ANY, 'wxButton',
    
     pos=(300, 150), size=(320, 250))
     self.SetBackgroundColour("green")
     self.button1 = wx.Button(self, id=-1, label='Button1',
     pos=(8, 8), size=(175, 28))
     self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
      # optional tooltip
      self.button1.SetToolTip(wx.ToolTip("click to hide"))
    
      # show the frame 
       self.Show(True)
        def button1Click(self,event):
        self.button1.Hide()
        self.SetTitle("Button1 clicked")
        self.button2.Show()
    
        application = wx.PySimpleApp()
        # call class MyFrame
        window = MyFrame()
        # start the event loop
        application.MainLoop()
    
    这只是设置带有复选框的小部件

    输出: 这很有效

    import wx
    
    class MyFrame(wx.Frame):
       """make a frame, inherits wx.Frame"""
    
    def __init__(self):
       # create a frame, no parent, default to wxID_ANY
    
     wx.Frame.__init__(self, None, wx.ID_ANY, 'wxButton',
    
     pos=(300, 150), size=(320, 250))
     self.SetBackgroundColour("green")
     self.button1 = wx.Button(self, id=-1, label='Button1',
     pos=(8, 8), size=(175, 28))
     self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
      # optional tooltip
      self.button1.SetToolTip(wx.ToolTip("click to hide"))
    
      # show the frame 
       self.Show(True)
        def button1Click(self,event):
        self.button1.Hide()
        self.SetTitle("Button1 clicked")
        self.button2.Show()
    
        application = wx.PySimpleApp()
        # call class MyFrame
        window = MyFrame()
        # start the event loop
        application.MainLoop()
    
    这只是设置带有复选框的小部件

    输出:

    您必须将其添加到面板或框架中,然后上述代码将被删除work@BinayakaChakraborty:请检查编辑,我做错了什么吗?检查稻草人的回答:)你必须将其添加到面板或框架中,然后上面的代码将work@BinayakaChakraborty:请检查编辑,我做错什么了吗?看看稻草人的回答:)