wxwidgets控件参考

wxPython常用控件–wx.Font,wx.StaticText,wx.StaticBitmap,wx.Button,wx.TextCtrl

https://blog.csdn.net/tianmaxingkong_/article/details/54345720

wxPython Widgets

http://xoomer.virgilio.it/infinity77/wxPython/widgets.html

关注公众号“码农帮派”,查看更多系列技术文章:

 

wxPython各种控件用法官方手册 : http://xoomer.virgilio.it/infinity77/wxPython/widgets.html

 

(0)字体,wx.Font, 构造函数:

 


 
  1. """
  2. __init__(self, int pointSize, int family, int style, int weight, bool underline=False,
  3. String face=EmptyString,
  4. int encoding=FONTENCODING_DEFAULT) -> Font
  5. """

 

 

font = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False)
 

其他的控件可以通过 SetFont(font)来设置自己字体属性。

 

 

(1)文字显示,wx.StaticText

用来显示静态文字内容,构造函数:

 


 
  1. """
  2. __init__(self, Window parent, int id=-1, String label=EmptyString,
  3. Point pos=DefaultPosition, Size size=DefaultSize,
  4. long style=0, String name=StaticTextNameStr) -> StaticText
  5. """

 

【说明】

通过调用StaticText对象的SetLabel()方法和SetValue()方法可以设置器显示的文字内容。

 

(2)图片显示,wx.StaticBItmap

用来显示一张静态的图片,构造函数:

 


 
  1. """
  2. __init__(self, Window parent, int id=-1, Bitmap bitmap=wxNullBitmap,
  3. Point pos=DefaultPosition, Size size=DefaultSize,
  4. long style=0, String name=StaticBitmapNameStr) -> StaticBitmap
  5. """

【说明】

 

通过调用StaticBitmap对象的SetBitmap()更换显示的图片,需要说明的是,wx.StaticBitmap对象没有自动适配图片大小的接口,需要程序增大缩小图片到合适的尺寸,然后通过SetBitmap()的方式显示图片:

 


 
  1. img_big = wx.Image("D:\icon\img_big.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  2. staticBmp = wx.StaticBitmap(self, -1, img_big, pos=(10, 10), size=(200, 200))
  3. staticBmp.SetBackgroundColour("#a8a8a8")
  4. # 重置Image对象尺寸的函数
  5. def resizeBitmap(image, width=100, height=100):
  6. bmp = image.Scale(width, height).ConvertToBitmap()
  7. return bmp
  8. img_ori = wx.Image("D:\icon\img_ori.png", wx.BITMAP_TYPE_ANY)
  9. staticBmp.SetBitmap(resizeBitmap(img_ori, 200, 200))

 

 

(3)按键,Button

 

(3-1)wx.Button,构造函数(在不同的平台上, e.g. windows, Linux, MacOS上样子不一样):

 


 
  1. """
  2. __init__(self, Window parent, int id=-1, String label=EmptyString,
  3. Point pos=DefaultPosition, Size size=DefaultSize,
  4. long style=0, Validator validator=DefaultValidator,
  5. String name=ButtonNameStr) -> Button
  6. """

 

 

 

 


 
  1. checkStudentScoreBtn = wx.Button(studentPanel, -1, u'查看成绩', pos=(120, 210), size=(150, 30))
  2. checkStudentScoreBtn.SetBackgroundColour("#0a74f7")
  3. checkStudentScoreBtn.SetFont(self.textFont)
  4. checkStudentScoreBtn.SetForegroundColour("white")

效果图:

 

(3-2)GenButton,通用Button,构造函数(在不同的平台上显示的样子相同):

 


 
  1. __init__(self, parent, id=-1, label='',
  2. pos = wx.DefaultPosition, size = wx.DefaultSize,
  3. style = 0, validator = wx.DefaultValidator,
  4. name = "genbutton")

【说明】

 

要使用GenButton,需要从wx.lib.buttons中导入:

 


 
  1. from wx.lib.buttons import GenButton as wxButton
  2. tmpButton = wxButton(parent, id, u'删除学生', pos=(10, 10), size=(100, 30), style=wx.BORDER_NONE)
  3. tmpButton.SetBackgroundColour("#ff0000")
  4. tmpButton.SetForegroundColour("#ffffff")

效果图:

 

 

Button的点击事件的触发:

 


 
  1. button = wx.Button(self, id=100, u'Click Me', pos=(120, 210), size=(150, 30))
  2. button.SetBackgroundColour("#0a74f7")
  3. button.SetFont(self.textFont)
  4. button.SetForegroundColour("white")
  5. self.Bind(wx.EVT_BUTTON, self.buttonClickFunc, button)
  6. def buttonClickFunc(self, event):
  7. btnID = event.GetButtonObj().GetId()
  8. print btnID # 此处打印结果 100
  9. pass

 

(3-3)图片和文字共同显示的Button

在wx.lib.button的苦衷提供了一个GenBitmapButton的按键,可以图片和文字共同显示,但经过尝试发现,图片和文字是并排显示的,而不是文字在图片正中,查看了GenBitmapButton的源码,重写了该Button:

xButton.py

 


 
  1. #coding=utf-8
  2. import wx
  3. from wx.lib.buttons import GenBitmapButton
  4. class GenBitmapTextButton(GenBitmapButton):
  5. """A generic bitmapped button with text label"""
  6. def __init__(self, parent, id=-1, bitmap=wx.NullBitmap, label='',
  7. pos = wx.DefaultPosition, size = wx.DefaultSize,
  8. style = 0, validator = wx.DefaultValidator,
  9. name = "genbutton"):
  10. GenBitmapButton.__init__(self, parent, id, bitmap, pos, size, style, validator, name)
  11. self.SetLabel(label)
  12. def _GetLabelSize(self):
  13. """ used internally """
  14. w, h = self.GetTextExtent(self.GetLabel())
  15. if not self.bmpLabel:
  16. return w, h, True # if there isn't a bitmap use the size of the text
  17. w_bmp = self.bmpLabel.GetWidth()
  18. h_bmp = self.bmpLabel.GetHeight()
  19. width = w + w_bmp
  20. if h_bmp > h:
  21. height = h_bmp
  22. else:
  23. height = h
  24. return width, height, True
  25. def DrawLabel(self, dc, width, height, dx=0, dy=0):
  26. bmp = self.bmpLabel
  27. if bmp is not None: # if the bitmap is used
  28. if self.bmpDisabled and not self.IsEnabled():
  29. bmp = self.bmpDisabled
  30. if self.bmpFocus and self.hasFocus:
  31. bmp = self.bmpFocus
  32. if self.bmpSelected and not self.up:
  33. bmp = self.bmpSelected
  34. bw,bh = bmp.GetWidth(), bmp.GetHeight()
  35. if not self.up:
  36. dx = dy = self.labelDelta
  37. hasMask = bmp.GetMask() is not None
  38. else:
  39. bw = bh = 0 # no bitmap -> size is zero
  40. dc.SetFont(self.GetFont())
  41. if self.IsEnabled():
  42. dc.SetTextForeground(self.GetForegroundColour())
  43. else:
  44. dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT))
  45. label = self.GetLabel()
  46. tw, th = dc.GetTextExtent(label) # size of text
  47. if not self.up:
  48. dx = dy = self.labelDelta
  49. pos_x = (width-bw)/2+dx # adjust for bitmap and text to centre
  50. if bmp is not None:
  51. dc.DrawBitmap(bmp, pos_x, (height-bh)/2+dy, hasMask) # draw bitmap if available
  52. pos_x = pos_x + 2 # extra spacing from bitmap
  53. dc.DrawText(label, bw/2-tw/2, bh/2-th/2) # draw the text

以后可以直接引入这个文件,直接使用GenBitmapTextButton了:

 

 


 
  1. from xButton import GenBitmapTextButton as StarButton
  2. startImage = wx.Image(r'D:\schedule_uf_icon.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  3. startButton = StarButton(parent=self, id=-1, label=str(1), bitmap=startImage, pos=(30, 30), size=(60, 60), style=wx.BORDER_NONE)
  4. startButton.SetFont(sFont)
  5. self.Bind(wx.EVT_BUTTON, clickEvent, startButton)

效果:

 

【说明】若要动态的设置GenBitmapText的背景图片:SetBitmapLabel(self, bitmap)来设置的

 

(4)文本输入框,wx.TextCtrl,构造函数:

 


 
  1. """
  2. __init__(self, Window parent, int id=-1, String value=EmptyString,
  3. Point pos=DefaultPosition, Size size=DefaultSize,
  4. long style=0, Validator validator=DefaultValidator,
  5. String name=TextCtrlNameStr) -> TextCtrl
  6. """

 

 

(4-1)普通的文本输入框

 


 
  1. self.accountInput = wx.TextCtrl(panle, -1, u'', pos=(80, 25), size=(180, -1))
  2. self.accountInput.SetForegroundColour('gray')
  3. self.accountInput.SetFont(font)

效果:

 

(4-2)密码输入框

 


 
  1. self.passwordInput = wx.TextCtrl(panle, -1, u'', pos=(80, 70), size=(180, -1), style=wx.TE_PASSWORD)
  2. self.passwordInput.SetForegroundColour('gray')

效果:

 

(4-3)多行输入框

 


 
  1. self.subjectContent = wx.TextCtrl(self, -1, "", pos=(120, 40), size=(425, 80), style=wx.TE_MULTILINE)
  2. self.subjectContent.SetForegroundColour("#000000")
  3. self.subjectContent.SetFont(self.textFont)

效果:

 

 

若要获得TextCtrl的输入内容:

 

content = textCtrl.GetValue()
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值