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

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

 

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

 

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

 

"""
__init__(self, int pointSize, int family, int style, int weight, bool underline=False, 
    String face=EmptyString, 
    int encoding=FONTENCODING_DEFAULT) -> Font
"""

 

 

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

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

 

 

(1)文字显示,wx.StaticText

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

 

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

 

【说明】

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

 

(2)图片显示,wx.StaticBItmap

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

 

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

【说明】

 

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

 

img_big = wx.Image("D:\icon\img_big.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
staticBmp = wx.StaticBitmap(self, -1, img_big, pos=(10, 10), size=(200, 200))
staticBmp.SetBackgroundColour("#a8a8a8")

# 重置Image对象尺寸的函数
def resizeBitmap(image, width=100, height=100):
    bmp = image.Scale(width, height).ConvertToBitmap()
    return bmp

img_ori = wx.Image("D:\icon\img_ori.png", wx.BITMAP_TYPE_ANY)
staticBmp.SetBitmap(resizeBitmap(img_ori, 200, 200))

 

 

(3)按键,Button

 

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

 

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

 

 

 

 

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

效果图:

 

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

 

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

【说明】

 

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

 

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

效果图:

 

 

Button的点击事件的触发:

 

button = wx.Button(self, id=100, u'Click Me', pos=(120, 210), size=(150, 30))
button.SetBackgroundColour("#0a74f7")
button.SetFont(self.textFont)
button.SetForegroundColour("white")
self.Bind(wx.EVT_BUTTON, self.buttonClickFunc, button)

def buttonClickFunc(self, event):
    btnID = event.GetButtonObj().GetId()
    print btnID # 此处打印结果 100
    pass

 

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

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

xButton.py

 

#coding=utf-8
import wx
from wx.lib.buttons import GenBitmapButton

class GenBitmapTextButton(GenBitmapButton):
    """A generic bitmapped button with text label"""
    def __init__(self, parent, id=-1, bitmap=wx.NullBitmap, label='',
                 pos = wx.DefaultPosition, size = wx.DefaultSize,
                 style = 0, validator = wx.DefaultValidator,
                 name = "genbutton"):
        GenBitmapButton.__init__(self, parent, id, bitmap, pos, size, style, validator, name)
        self.SetLabel(label)


    def _GetLabelSize(self):
        """ used internally """
        w, h = self.GetTextExtent(self.GetLabel())
        if not self.bmpLabel:
            return w, h, True       # if there isn't a bitmap use the size of the text

        w_bmp = self.bmpLabel.GetWidth()
        h_bmp = self.bmpLabel.GetHeight()
        width = w + w_bmp
        if h_bmp > h:
            height = h_bmp
        else:
            height = h
        return width, height, True


    def DrawLabel(self, dc, width, height, dx=0, dy=0):
        bmp = self.bmpLabel
        if bmp is not None:     # if the bitmap is used
            if self.bmpDisabled and not self.IsEnabled():
                bmp = self.bmpDisabled
            if self.bmpFocus and self.hasFocus:
                bmp = self.bmpFocus
            if self.bmpSelected and not self.up:
                bmp = self.bmpSelected
            bw,bh = bmp.GetWidth(), bmp.GetHeight()
            if not self.up:
                dx = dy = self.labelDelta
            hasMask = bmp.GetMask() is not None
        else:
            bw = bh = 0     # no bitmap -> size is zero

        dc.SetFont(self.GetFont())
        if self.IsEnabled():
            dc.SetTextForeground(self.GetForegroundColour())
        else:
            dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT))

        label = self.GetLabel()
        tw, th = dc.GetTextExtent(label)        # size of text
        if not self.up:
            dx = dy = self.labelDelta

        pos_x = (width-bw)/2+dx      # adjust for bitmap and text to centre
        if bmp is not None:
            dc.DrawBitmap(bmp, pos_x, (height-bh)/2+dy, hasMask) # draw bitmap if available
            pos_x = pos_x + 2   # extra spacing from bitmap

        dc.DrawText(label, bw/2-tw/2, bh/2-th/2)      # draw the text

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

 

 

from xButton import GenBitmapTextButton as StarButton

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

效果:

 

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

 

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

 

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

 

 

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

 

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

效果:

 

(4-2)密码输入框

 

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

效果:

 

(4-3)多行输入框

 

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

效果:

 

wxPython常用控件--wx.Font,wx.StaticText,wx.StaticBitmap,wx.Button,wx.TextCtrl_第1张图片

 

若要获得TextCtrl的输入内容:

 

content = textCtrl.GetValue()

 

 

 

 

你可能感兴趣的:(Python)