wxpython中设置radiobox相关使用

#coding=utf-8

import wx

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Wxpython Radiobox 演示",size = (800,600))
        panel = wx.Panel(self)
        #第一种方法使用wx.RadioButton类
        #RadioButton(parent, id=ID_ANY, label=EmptyString, 
        #            pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=RadioButtonNameStr)
        self.check1 = wx.RadioButton(panel,-1,"Value1",pos = (50,20),style = wx.RB_GROUP)
        self.check2 = wx.RadioButton(panel, -1, "Value2", pos=(150,20))
        self.check3 = wx.RadioButton(panel, -1, "Value3", pos=(250,20))
        self.check1.Bind(wx.EVT_RADIOBUTTON,self.Event1)
        self.check2.Bind(wx.EVT_RADIOBUTTON,self.Event1)
        self.check3.Bind(wx.EVT_RADIOBUTTON,self.Event1)
        
        self.check4 = wx.RadioButton(panel, -1, "Value2-1", pos=(50,60), style=wx.RB_GROUP)
        self.check5 = wx.RadioButton(panel, -1, "Value2-2", pos=(150,60))
        self.check6 = wx.RadioButton(panel, -1, "Value2-3", pos=(250,60))
        self.check4.Bind(wx.EVT_RADIOBUTTON,self.Event2)
        self.check5.Bind(wx.EVT_RADIOBUTTON, self.Event2)
        self.check6.Bind(wx.EVT_RADIOBUTTON, self.Event2)
        
        
        #第二种方法使用wx.RadioBox类
        list3 = ["Value3-1","Value3-2" ,"Value3-3"]
        #RadioBox(parent, id=ID_ANY, label=EmptyString, pos=DefaultPosition, size=DefaultSize, 
        #         choices=[], majorDimension=0, style=RA_SPECIFY_COLS, validator=DefaultValidator, name=RadioBoxNameStr)               
        self.radiobox3 = wx.RadioBox(panel,-1,"测试3",pos=(50,100),choices=list3,style=wx.RA_SPECIFY_COLS)
        self.radiobox3.Bind(wx.EVT_RADIOBOX,self.Event3)
        
        list4 = ["Value4-1","Value4-2" ,"Value4-3"]
        self.radiobox4 = wx.RadioBox(panel, -1, "测试4",pos=(50, 180),choices=list4,  style=wx.RA_SPECIFY_ROWS)
        self.radiobox4.Bind(wx.EVT_RADIOBOX, self.Event4)
        
        #设定初始值,按序号进行选择,序号从0开始计数
        self.radiobox3.SetSelection(2)
        
        #方式2,设定初始值,通过文本进行选择
        self.radiobox4.SetSelection(self.radiobox4.FindString("Value4-3"))

    def Event1(self,event):
        print "测试1:",self.check1.GetLabel()

    def Event2(self,event):
        print "测试2:", self.check4.GetLabel()

    def Event3(self,event):
        #方式1获取选中项
        print "测试3:",self.radiobox3.GetStringSelection(),self.radiobox3.GetSelection()
        #方式2获取选中项,更灵活
        print "测试3.1",event.GetString(),event.GetInt()


    def Event4(self,event):
        print "测试4:",self.radiobox4.GetStringSelection()

if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

不排版了,直接放代码,干货如下:

1、示范了两种写法,方法二更简单。

2、示范了如何在点击事件中获取 选中项

3、示范如何设置初始值,尤其是根据文本选中,折磨了我好长时间,在源码里猜出来的。

posted @ 2019-05-23 23:42  黯然销魂掌2015  阅读(2496)  评论(0编辑  收藏  举报

联系方式:qq 16906913