天天看点

wxPython控件学习之StaticText静态文本框

<code>#! /usr/bin/env python #coding:UTF-8 # #wx静态文本使用范例 __author__="[email protected]" __date__ ="$2010-5-14 9:10:52$" import wx #窗口类 class StaticTextFrame(wx.Frame):     def __init__(self):         wx.Frame.__init__(self,None,-1,"Example For StaticText",size=(400,300))         panel=wx.Panel(self,-1)         #基本静态文本         wx.StaticText(panel,-1,"This is a basic StaticText Example",(100,10))         #指定前景色和背景色         stext=wx.StaticText(panel,-1,"StaticText with Fg and Bg Colour",(100,30))         stext.SetForegroundColour("White")         stext.SetBackgroundColour("Black")         #指定居中对齐         centerText=wx.StaticText(panel,-1,"Align Center",(100,50),(160,-1),                                         wx.ALIGN_CENTER)         centerText.SetForegroundColour("White")         centerText.SetBackgroundColour("Black")         #指定右对齐         rightText=wx.StaticText(panel,-1,"Align Right",(100,70),(160,-1),                                         wx.ALIGN_RIGHT)         rightText.SetForegroundColour("White")         rightText.SetBackgroundColour("Black")         #指定字体的静态文本         str="You can change the font"         fontText=wx.StaticText(panel,-1,str,(20,100))         font=wx.Font(18,wx.DECORATIVE,wx.ITALIC,wx.NORMAL)         fontText.SetFont(font)                  #设置多行文本         mutiStr="Here we go\nYou can see\nThe ending!"         wx.StaticText(panel,-1,mutiStr,(20,150))                  #设置多行文本右对齐         mutiRightStr="Example again\n But this time\nWe Align Right!"         wx.StaticText(panel,-1,mutiRightStr,(220,150),                       style=wx.ALIGN_RIGHT) def main():     app=wx.PySimpleApp()     frame=StaticTextFrame()     frame.Show(True)     app.MainLoop() if __name__ == "__main__":     main()</code>

wx.StaticText控件的构造函数参数有:

---------------------------------------------------

parent:父窗口部件。

id:标识符。使用-1可以自动创建一个唯一的标识。

label:你想显示在静态控件中的文本。

pos:一个wx.Point或一个Python元组,它是窗口部件的位置。

size:一个wx.Size或一个Python元组,它是窗口部件的尺寸。

style:样式标记。

name:对象的名字,用于查找的需要。

其中样式style的取值有:

wx.ALIGN_CENTER:静态文本位于静态文本控件的中心。

wx.ALIGN_LEFT:文本在窗口部件中左对齐。这是默认的样式。

wx.ALIGN_RIGHT:文本在窗口部件中右对齐。

wx.ST_NO_AUTORESIZE:如果使用了这个样式,那么在使用了SetLabel()改变文

本之后,静态文本控件不将自我调整尺寸。你应结合使用一个居中或右对齐的控件来

保持对齐。