按钮控件

来源:互联网 发布:西科软件招聘 编辑:程序博客网 时间:2024/04/29 16:49

普通按钮

# -*- coding: UTF-8 -*-import wxclass ButtonFrame(wx.Frame):    def __init__(self):        wx.Frame.__init__(self, None, -1, u'按钮', size=(300, 90)) #父类构造函数        panel = wx.Panel(self, -1)        self.button = wx.Button(panel, -1, u"确定", pos=(10, 10)) # 创建按钮对象,按钮标签为中文“确定”故前加u        self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)                 #为按钮绑定onclick事件处理函数        self.button.SetDefault()                                       # 设置button为默认按钮        self.inputText = wx.TextCtrl(panel, -1, "", pos = (100,10), size=(150, -1), style=wx.TE_READONLY)        #style=wx.TE_READONLY   表示创建只读文本框控件    def OnClick(self, event):    #event表示单击        self.inputText.Value = "Hello world!"   #单击确定时,只读文本框inputText将出现值"Hello worldif __name__ == '__main__':    app = wx.PySimpleApp()    frame = ButtonFrame()    frame.Show()    app.MainLoop()

效果,单击确定,文本框显示helloworld



位图按钮

创建位图按钮前要确定image类型的格式,并将其转换位bitmap类型,再创建位图按钮,并关联bitmap的实例化对象

import wxclass BitmapButtonFrame(wx.Frame):    def __init__(self):        wx.Frame.__init__(self, None, -1, u'位图按钮', size=(200, 150))        panel = wx.Panel(self, -1)        bmp = wx.Image("button.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()        #bmp为wx.Image的对象。 wx.Image("button.png", wx.BITMAP_TYPE_PNG)表示图片类型为PNG格式        #ConvertToBitmap()将image转换为Bitmap类型对象        self.button = wx.BitmapButton(panel, -1, bmp, pos=(20, 20), size=(120, 60))        #创建BitmapButton类的对象button,并关联了前面的bmp对象        self.Bind(wx.EVT_BUTTON, self.OnClick, self.button) #设置了按钮单击事件和处理函数OnClick        self.button.SetDefault()    def OnClick(self, event):        wx.MessageBox("Hello world!", u"提示")  #单击按钮后弹出对话框 内容为Hello world!。对话框标题为“提示”if __name__ == '__main__':    app = wx.PySimpleApp()    frame = BitmapButtonFrame()    frame.Show()    app.MainLoop()

结果:单击位图




0 0
原创粉丝点击