例子
在以下示例中,在面板的垂直框尺寸器中添加了一个水平 Gauge 对象。
self.gauge = wx.Gauge(pnl, range = 20, size = (250, 25), style = wx.GA_HORIZONTAL)
还有一个按钮,其单击事件与处理函数相关联。
self.btn1 = wx.Button(pnl, label = "Start")
self.Bind(wx.EVT_BUTTON, self.OnStart, self.btn1)
处理函数 OnStart() 每秒钟更新一次仪表的进度。
def OnStart(self, e):
while True:
time.sleep(1);
self.count = self.count + 1
self.gauge.SetValue(self.count)
if self.count >= 20:
print "end"
return
该示例的完整代码如下 -
import wx
import time
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (300,200))
self.InitUI()
def InitUI(self):
self.count = 0
pnl = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
self.gauge = wx.Gauge(pnl, range = 20, size = (250, 25), style = wx.GA_HORIZONTAL)
self.btn1 = wx.Button(pnl, label = "Start")
self.Bind(wx.EVT_BUTTON, self.OnStart, self.btn1)
hbox1.Add(self.gauge, proportion = 1, flag = wx.ALIGN_CENTRE)
hbox2.Add(self.btn1, proportion = 1, flag = wx.RIGHT, border = 10)
vbox.Add((0, 30))
vbox.Add(hbox1, flag = wx.ALIGN_CENTRE)
vbox.Add((0, 20))
vbox.Add(hbox2, proportion = 1, flag = wx.ALIGN_CENTRE)
pnl.SetSizer(vbox)
self.SetSize((300, 200))
self.Centre()
self.Show(True)
def OnStart(self, e):
while True:
time.sleep(1);
self.count = self.count + 1
self.gauge.SetValue(self.count)
if self.count >= 20:
print "end"
return
ex = wx.App()
Mywin(None,'wx.Gauge')
ex.MainLoop()
上面的代码产生以下输出 -