静态文本控件
大概对于所有的UI 工具来说,最基本的任务就是在屏幕上绘制纯文本。在wxPython 中,使用类wx.StaticText 来完成。

废话先不说,给出代码:
#
-*- coding: GBK -*-
import wx
class StaticTextFrame(wx.Frame):
def __init__ (self):
wx.Frame. __init__ (self, None, - 1 , ' 显示静态文本 ' ,
size = ( 400 , 300 ))
panel = wx.Panel(self, - 1 )
# 这是一个基本的静态文本
wx.StaticText(panel, - 1 , " 这是一个基本的静态文本 " ,
( 100 , 10 ))
# 指定了前景色和背景色的静态文本
rev = wx.StaticText(panel, - 1 , " 指定了前景色和背景色的静态文本 " ,
( 100 , 30 ))
rev.SetForegroundColour( ' white ' )
rev.SetBackgroundColour( ' black ' )
# 指定居中对齐的的静态文本
center = wx.StaticText(panel, - 1 , " 居中,白字,黑色背景 " , ( 100 , 50 ),
( 160 , - 1 ), wx.ALIGN_CENTER)
center.SetForegroundColour( ' white ' )
center.SetBackgroundColour( ' black ' )
# 指定右对齐的静态文本
right = wx.StaticText(panel, - 1 , " 文本右对齐 " , ( 100 , 70 ),
( 160 , - 1 ), wx.ALIGN_RIGHT)
right.SetForegroundColour( ' white ' )
right.SetBackgroundColour( ' black ' )
# 指定新字体的静态文本
str = " You can also change the font. "
text = wx.StaticText(panel, - 1 , str, ( 20 , 100 ))
font = wx.Font( 18 , wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
text.SetFont(font)
# 显示多行文本
wx.StaticText(panel, - 1 , " 你的文本\n可以分成 "
" 多行显示,\n牛逼不? " , ( 20 , 150 ))
# 显示对齐的多行文本
wx.StaticText(panel, - 1 , " 你的文本\n可以分成 "
" 多行显示\n牛逼不?\n还可以设置字体和对齐? " , ( 220 , 150 ),
style = wx.ALIGN_RIGHT)
if __name__ == ' __main__ ' :
app = wx.PySimpleApp()
frame = StaticTextFrame()
frame.Show()
import wx
class StaticTextFrame(wx.Frame):
def __init__ (self):
wx.Frame. __init__ (self, None, - 1 , ' 显示静态文本 ' ,
size = ( 400 , 300 ))
panel = wx.Panel(self, - 1 )
# 这是一个基本的静态文本
wx.StaticText(panel, - 1 , " 这是一个基本的静态文本 " ,
( 100 , 10 ))
# 指定了前景色和背景色的静态文本
rev = wx.StaticText(panel, - 1 , " 指定了前景色和背景色的静态文本 " ,
( 100 , 30 ))
rev.SetForegroundColour( ' white ' )
rev.SetBackgroundColour( ' black ' )
# 指定居中对齐的的静态文本
center = wx.StaticText(panel, - 1 , " 居中,白字,黑色背景 " , ( 100 , 50 ),
( 160 , - 1 ), wx.ALIGN_CENTER)
center.SetForegroundColour( ' white ' )
center.SetBackgroundColour( ' black ' )
# 指定右对齐的静态文本
right = wx.StaticText(panel, - 1 , " 文本右对齐 " , ( 100 , 70 ),
( 160 , - 1 ), wx.ALIGN_RIGHT)
right.SetForegroundColour( ' white ' )
right.SetBackgroundColour( ' black ' )
# 指定新字体的静态文本
str = " You can also change the font. "
text = wx.StaticText(panel, - 1 , str, ( 20 , 100 ))
font = wx.Font( 18 , wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
text.SetFont(font)
# 显示多行文本
wx.StaticText(panel, - 1 , " 你的文本\n可以分成 "
" 多行显示,\n牛逼不? " , ( 20 , 150 ))
# 显示对齐的多行文本
wx.StaticText(panel, - 1 , " 你的文本\n可以分成 "
" 多行显示\n牛逼不?\n还可以设置字体和对齐? " , ( 220 , 150 ),
style = wx.ALIGN_RIGHT)
if __name__ == ' __main__ ' :
app = wx.PySimpleApp()
frame = StaticTextFrame()
frame.Show()
app.MainLoop()
我在这里遇到了编辑器中不能保存中文字符的问题,在第一行加上:# -*- coding: GBK -*-
就可以使用中文字符了。
下面详细分析代码
类StaticTextFrame为主窗口,在Python中称为Frame(框架)。他继承与wx.Frame
__init__函数为每个类的初始化函数,在类被初始化的时候自动触发。
wx.Frame.__init__(self, None, -1, '显示静态文本', size=(400, 300))
Frame的参数形式为:(self,parent,id,title,pos,size,style,name)
self
parent-该控件的父控件,即该控件存在于哪个子控件中
id-控件的id,实际开发过程中最好设置一个,便于在各类间进行操作
title-Frame标题栏中的文本
pos-控件所在位置,以坐标的形式表示,如:(10,10)
size-控件的大小,以长宽的形式表示:(100,30)
style-控件的样式,可以设置多个,用竖线(|)分开
name-控件的名称,用于查找的需要。
style参数的可用类型:
ALIGN_NOT
ALIGN_CENTER_HORIZONTAL
ALIGN_CENTRE_HORIZONTAL
ALIGN_LEFT
ALIGN_TOP
ALIGN_RIGHT
ALIGN_BOTTOM
ALIGN_CENTER_VERTICAL
ALIGN_CENTRE_VERTICAL
ALIGN_CENTER
ALIGN_CENTRE
ALIGN_MASK
wx.StaticText控件的参数:(self,parent,id,label,pos,size,style, name)
除label之外,其他参数和Frame控件相同
label-StatixText的文本内容
更多信息请访问www.njxsw.com