wxpython:wx.StaticText文本实现按钮效果及详细使用

一、wx.StaticText的使用

        在wx.StaticText中,你能够改变文本的对齐方式、字体和颜色。简单的静态文本控件可以包含多行文本,但是你不能处理多种字体或样式。处理多种字体或样式,要使用更精细的文本控件,如wx.html.HTMLWindow。为了在静态文本控件中显示多行文本,我们要包括其中有换行符的字符串,并使控件的大小足够显示所有的文本。,那就是wx.StaticText窗口不会接受或响应鼠标事件。

1.1、wx.StaticText构造函数的参数

parent:父窗口部件。

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

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

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

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

style:样式标记。

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

 1.2、样式

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

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

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

wx.ST_NO_AUTORESIZE:如果使用了这个样式,那么在使用了SetLabel()改变文本之后,静态文本控件不将自我调整尺寸。你应结合使用一个居中或右对齐的控件来保持对齐。

wx.StaticText控件覆盖了SetLabel(),以便根据新的文本来调整自身,除非wx.ST_NO_AUTORESIZE样式被设置了。

        当创建了一个居中或右对齐的单行静态文本时,你应该显式地在构造器中设置控件的尺寸。指定尺寸以防止wxPython自动调整该控件的尺寸。wxPython的默认尺寸是刚好包容了文本的矩形尺寸,因此对齐就没有什么必要。要在程序中动态地改变窗口部件中的文本,而不改变该窗口部件的尺寸,就要设置wx.ST_NO_AUTORESIZE样式。这样就防止了在文本被重置后,窗口部件自动调整尺寸到刚好包容了文本。如果静态文本是位于一个动态的布局中,那么改变它的尺寸可能导致屏幕上其它的窗口部件移动,这就对用户产生了干扰。

二、创建文本

创建一个添加成员的文本,具体文本方法使用

a_text = wx.StaticText(panel, -1, label=u'添加成员')
font_2 = wx.Font(10, family=wx.DECORATIVE, style=wx.NORMAL, weight=wx.NORMAL, faceName="STHupo")
self.new_user.SetCursor(wx.Cursor(wx.CURSOR_HAND))  # 附加点击效果
self.new_user.SetFont(font_2)

实现效果

"class MyDialog41(wx.Dialog): def init(self, parent): wx.Dialog.init(self, parent, id=wx.ID_ANY, title=u"订单信息", pos=wx.DefaultPosition, size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE) self.Center() self.panel = wx.Panel(self) self.panel.SetBackgroundColour('white') wx.StaticText(self.panel, -1, "买家电话:", (20, 20)) self.t1 = wx.TextCtrl(self.panel, pos=(90, 20), size=(120, 25)) wx.StaticText(self.panel, -1, "客服人员编号", (20, 60)) wx.StaticText(self.panel, -1, "订单编号", (100, 60)) wx.StaticText(self.panel, -1, "订单金额", (180, 60)) wx.StaticText(self.panel, -1, "订餐方式", (260, 60)) wx.StaticText(self.panel, -1, "食物名称", (340, 60)) wx.StaticText(self.panel, -1, "地址", (420, 60)) wx.StaticText(self.panel, -1, "份数", (500, 60)) def OnClick(self, e): dialog41 = MyDialog41(None) btn = wx.Button(parent=dialog41.panel, label="查询", pos=(240, 20), size=(70, 25)) btn.Bind(wx.EVT_BUTTON, dialog41.find) dialog41.ShowModal() def find(self, event): conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='wm', charset='utf8') cursor = conn.cursor() try: sql = "select * from book" cursor.execute(sql) rs = cursor.fetchall() h = 80 for row in rs: if row[0] == self.t1.GetValue(): h = h + 20 server_id = row[1] order_id = row[2] order_money = row[3] order_way = row[4] name_way = row[5] local_way = row[6] count_way = row[7] wx.StaticText(self.panel, -1, server_id, (20, h)) wx.StaticText(self.panel, -1, order_id, (100, h)) wx.StaticText(self.panel, -1, order_money, (180, h)) wx.StaticText(self.panel, -1, order_way, (260, h)) wx.StaticText(self.panel, -1, name_way, (340, h)) wx.StaticText(self.panel, -1, local_way, (420, h)) wx.StaticText(self.panel, -1, count_way, (500, h)) except: conn.rollback() finally: cursor.close() conn.close()"逐行解释代码
06-07
“ class MyDialog21(wx.Dialog): def __init__(self, parent): wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title=u"派送员信息", pos=wx.DefaultPosition, size=wx.Size(400, 415), style=wx.DEFAULT_DIALOG_STYLE) self.Center() self.panel = wx.Panel(self) self.panel.SetBackgroundColour('white') wx.StaticText(self.panel, -1, "菜品名称:", (20, 20)) self.t1 = wx.TextCtrl(self.panel, pos=(90, 20), size=(120, 25)) # btn = wx.Button(parent=self.panel, label="查询", pos=(240, 20), size=(70, 25)) # btn.Bind(wx.EVT_BUTTON, self.find) wx.StaticText(self.panel, -1, "派送员编号", (20, 60)) wx.StaticText(self.panel, -1, "派送员姓名", (120, 60)) wx.StaticText(self.panel, -1, "派送员电话", (220, 60)) def OnClick(self, event): dialog21 = MyDialog21(None) btn = wx.Button(parent=dialog21.panel, label="查询", pos=(240, 20), size=(70, 25)) btn.Bind(wx.EVT_BUTTON, dialog21.find) dialog21.ShowModal() def find(self, event): conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='wm', charset='utf8') cursor = conn.cursor() try: sql = "select * from courier" cursor.execute(sql) rs = cursor.fetchall() h = 80 for row in rs: if row[3] == self.t1.GetValue(): h = h + 20 courier_id = row[0] courier_name = row[1] courier_phone = row[2] wx.StaticText(self.panel, -1, courier_id, (20, h)) wx.StaticText(self.panel, -1, courier_name, (120, h)) wx.StaticText(self.panel, -1, courier_phone, (220, h)) except: conn.rollback() finally: cursor.close() conn.close()”逐行解释代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值