人生苦短,我学Python---------wxPython图形用户界面编程(wxPython控件之按钮)

wxPython中的按钮主要有三种:

        1、普通按钮 wx.Button

        2、位图按钮 wx.BitmapButton

        3、开关按钮 wx.ToggleButton

通用按钮:


import wx
import wx.lib.buttons as buttons
# 自定义窗口类
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='第一个wxPython程序', size=(400, 300), pos=(100, 100))
        # 窗口居中
        self.Center()
        panel = wx.Panel(parent=self)
        # wx.Button(parent, id, label, pos, size=wxDefaultSize, style=0, validaor, name="button)
        # 普通按钮
        self.button = wx.Button(panel, -1, "Hello", pos=(100, 100))
        self.button.SetForegroundColour("black")
        self.button.SetBackgroundColour("red")
        self.button.SetDefault() # 设置该按钮为默认按钮
        print(self.button.GetDefaultSize()) # 获得系统默认的按钮尺寸
        # 获取按钮上的文字 GetLabel()  设置按钮上的文字 SetLabel()
        # style   wx.BU_EXACTFIT 按钮尺寸为刚好容纳标签  wx.BU_LEFT wx.BU_RIGHT wx.BU_TOP wx.BU_BOTTOM 改变标签的对齐方式


        # 位图按钮
        bmp = wx.Image("C:/Users/admin/Desktop/14.jpg", wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        # 默认style=wx.BU_AUTODRAW 带有3D边框,看起来像文本按钮 令style=0即可
        self.button1 = wx.BitmapButton(panel, -1, bmp, pos=(200, 100))
        self.button2 = wx.BitmapButton(panel, -1, bmp, pos=(600, 200), style=0)
        self.button1.SetBitmapDisabled(bmp)
        self.button1.SetBitmapFocus(bmp)
        self.button1.SetBitmapLabel(bmp)


        # 开关按钮
        # 该按钮发送EVT_TOGGLEBUTTON事件,与上面两个不同
        self.button3 = wx.ToggleButton(panel, -1, "toggleBtn")
        print(self.button3.GetValue())
        # 通过GetValue() SetValue()查看、设置ToggleButton的状态



        # 为什么要使用通用按钮?
        # 1、通用按钮比本地按钮具有更好的跨平台的外观。另一方面,通用按钮可能在具体的系统上看起来与本地按钮有些微的不同。
        # 2、使用通用按钮,你对它的外观有更多的控制权,并且能改变属性,如3D斜面的宽度和颜色,而这对于本地控件可能是不允许的。
        # 3、通用按钮类允许特性的合并,而wxWidget按钮不行。比如GenBitmapTextButton允许文本标签和位图的组合,GenBitmapToggleButton
        # 实现一个位图切换按钮。
        # 4、如果你正在创建一个按钮类,使用通用按钮是较容易的。由于其代码和参数是用Python
        # 写的,所以当创建一个新的子类的时候,对于检查和覆盖,它们的可用性更好。

        #TODO

class GenericButtonFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Generic Button Example',
        size=(500, 350))
        panel = wx.Panel(self, -1)
        sizer = wx.FlexGridSizer(4, 4, 20, 20)
        b = wx.Button(panel, -1, "A wx.Button")
        b.SetDefault()
        sizer.Add(b)
        b = wx.Button(panel, -1, "non-default wx.Button")
        sizer.Add(b)
        sizer.Add((10,10))
        b = buttons.GenButton(panel, -1, 'Genric Button')#基本的通用按钮
        sizer.Add(b)
        b = buttons.GenButton(panel, -1, 'disabled Generic')#无效的通用按钮
        b.Enable(False)
        sizer.Add(b)
        b = buttons.GenButton(panel, -1, 'bigger')#自定义尺寸和颜色的按钮
        b.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False))
        b.SetBezelWidth(2)
        b.SetBackgroundColour("Navy")
        b.SetForegroundColour("white")
        b.SetToolTip("This is a BIG button...")
        sizer.Add(b)
        bmp = wx.Image("C:/Users/admin/Desktop/15.jpg", wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        b = buttons.GenBitmapButton(panel, -1, bmp)#通用位图按钮
        sizer.Add(b)
        b = buttons.GenBitmapToggleButton(panel, -1, bmp)#通用位图开关按钮
        sizer.Add(b)
        b = buttons.GenBitmapTextButton(panel, -1, bmp, "Bitmapped Text",
        size=(175, 75))#位图文本按钮
        b.SetUseFocusIndicator(False)
        sizer.Add(b)
        b = buttons.GenToggleButton(panel, -1, "Toggle Button")#通用开关按钮
        sizer.Add(b)
        panel.SetSizer(sizer)


class App(wx.App):
    def OnInit(self):
        # 创建窗口对象
        frame = MyFrame()
        frame1 = GenericButtonFrame()
        frame.Show()
        frame1.Show()
        return True

    def OnExit(self):
        print('退出程序!')
        return 0


if __name__ == '__main__':
    # 创建应用程序对象
    app = App()
    # 进入主事件循环
    app.MainLoop()

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值