wxpython中文教程之核心部件wx.gauge

发布时间:2020-10-12编辑:脚本学堂
有关wxpython核心部件wx.gauge的用法,wx.gauge用于处理长任务,wx.gauge的实例代码,wxpython中文教程学习,需要的朋友参考下。

wx.Gauge 是一个用在有漫长任务要处理时 (比如程序安装、导出 PDF 等)。

专题教程:wxpython中文教程

wx.Gauge 样式有:
wx.GA_HORIZONTAL
wx.GA_VERTICAL

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#gauge.py

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.timer = wx.Timer(self, -1)
        self.count = 0
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
       
        self.gauge = wx.Gauge(panel, -1, 50, size=(250, 25))
        self.btn1 = wx.Button(panel, wx.ID_OK)
        self.btn2 = wx.Button(panel, wx.ID_STOP)
        self.text = wx.StaticText(panel, -1, '任务有待完成')
       
        self.Bind(wx.EVT_BUTTON, self.OnOk, self.btn1)
        self.Bind(wx.EVT_BUTTON, self.OnStop, self.btn2)
       
        hbox1.Add(self.gauge, 1, wx.ALIGN_CENTER)
        hbox2.Add(self.btn1, 1, wx.RIGHT, 10)
        hbox2.Add(self.btn2, 1)
        hbox3.Add(self.text, 1)
        vbox.Add((0, 50), 0)
        vbox.Add(hbox1, 0, wx.ALIGN_CENTER)
        vbox.Add((0, 30), 0)
        vbox.Add(hbox2, 1, wx.ALIGN_CENTER)
        vbox.Add(hbox3, 1, wx.ALIGN_CENTER)
        panel.SetSizer(vbox)
        self.Center()
       
    def OnOk(self, event):
        if self.count >= 50:
            return
        self.timer.Start(100)
        self.text.SetLabel('任务正在进行')

    def OnStop(self, event):
        if self.count == 0 or self.count >= 50 or not
        self.timer.IsRunning():
            return
        self.timer.Stop()
        self.text.SetLabel('任务被中断')
        wx.Bell()
       
    def OnTimer(self, event):
        self.count = self.count+1
        self.gauge.SetValue(self.count)
        if self.count == 50:
            self.timer.Stop()
            self.text.SetLabel('任务已完成')
           
class MyApp(wx.App):
    def OnInit(self):
        frame=MyFrame(None, -1, 'gauge.py')
        frame.Show(True)
        return True

app=MyApp(0)
app.MainLoop()