1
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       
4

2 に答える 2

4

これは機能します。

import wx

class MyApp(wx.App):

  def OnInit(self):
    frame = InsertFrame(parent=None, id=-1)
    frame.Show()
    return True

class InsertFrame(wx.Frame):

  def __init__(self, parent, id):
    wx.Frame.__init__(self, parent, id, 'Test Frame', size = (300,100))
    panel = wx.Panel(self)
    pos_y = 0
    for i in range(50):
      pos_y += 20
      cb = wx.CheckBox(panel, label="sample checkbox", pos=(20, pos_y))

if __name__ == "__main__":
  app = MyApp()
  app.MainLoop()

これは、チェックボックスを使用してウィジェットをセットアップするだけです。

出力: そして出力:

于 2013-06-19T07:54:43.567 に答える
1
  1. チェックボックス クラス -> http://wxpython.org/docs/api/wx.CheckBox-class.html

  2. ボタン クラス -> http://wxpython.org/docs/api/wx.Button-class.html

  3. チェックボックスのコード例:

      #!/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()   
    
  4. ボタンのコード例:

    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()
    
  5. 他のすべての wxpython ウィジェットの優れたチュートリアル: (wx.Button wx.ToggleButton、wx.StaticLine、wx.StaticText、wx.StaticBox wx.ComboBox、wx.CheckBox、wx.StatusBar、wx.RadioButton、wx.Gauge、wx.スライダーと wx.SpinCtrl) -> http://zetcode.com/wxpython/widgets/

于 2013-06-19T06:17:41.463 に答える