1. 程式人生 > >wxPython常用控制元件--wx.Font,wx.StaticText,wx.StaticBitmap,wx.Button,wx.TextCtrl

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

(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)
效果:


若要獲得TextCtrl的輸入內容:

content = textCtrl.GetValue()