wxPython实现带颜色的进度条

  

本文实例为大家分享了wxPython实现带颜色进度条的具体代码,供大家参考,具体内容如下

  

<强>【问题描述】

  

1,在使用wxpython创建进度条时遇到如下问题,使用SetForegroundColour和SetBackgrounColour指令在Win7下不生效,即无法改变进度条的颜色及其背景颜色。

  

2,查阅资料发现wx.lib.agw.pygauge可以实现进度条颜色的修改(具体指令请参考pygauge说明文档),但其显示效果为平面较差(如下图):

  

 wxPython实现带颜色的进度条

  

图1 - 1 pygauge制作的进度条

  

期望实现的进度条效果如下:

  

 wxPython实现带颜色的进度条

  

图1 - 2期望效果图

  

<强>【程序代码】

        # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #   #计wxPython   #   #金表示,@ 2013年10月25日   #邮箱:jianchaojin@gmail.com   #   #最后的评论   # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #      ”“”   描述===========计与wxpython画颜色   使用=====使用例子::   进口的天气   导入表   类MyFrame (wx.Frame):   def __init__(自我、父母):   wx.Frame。__init__(自我,父母,1,“ColourGauge演示”)   面板=wx.Panel(自我)      gauge1=计。ColourGauge(面板1大?25 (100))   gauge1.setPercent (0.8)   gauge1.setBarColour (wx.RED)   gauge1.setBackgroundColour (wx.WHITE)      gauge2=计。ColourGauge(面板、1、大?(200年,50))   gauge2.setPercent (0.9)   gauge2.setBarColour (wx.RED)   gauge2.setBackgroundColour (wx.BLUE)   筛选器=wx.BoxSizer (wx.VERTICAL)   筛选器。Add (gauge1 0 wx.ALIGN_CENTER_VERTICAL | wx。,20)   筛选器。Add (gauge2 0 wx.ALIGN_CENTER_VERTICAL | wx。,20)      panel.SetSizer(筛选器)   sizer.Layout ()   #我们正常wxApp-derived类,像往常一样   应用=wx.App (0)   ?MyFrame(无)   app.SetTopWindow(框架)   frame.Show ()   app.MainLoop ()   ”“”      进口的天气   进口wx.lib。fancytext作为fancytext      类ColourGauge (wx.Panel):      def __init__(自我、父母,id、pos=wx。DefaultPosition大?wx.DefaultSize):   wx.Panel。__init__(自我,id=id,父母=父母,pos=pos,大?大小风格=wx.TAB_TRAVERSAL)   #创造计   自我。计=计(自我,id=1,尺寸大?)   self.SetAutoLayout(真正的)   lc=wx.LayoutConstraints ()   lc.top。篇(自我,天气。,0)# AsIs ()   lc.left.AsIs ()   lc.height.AsIs ()   lc.right。篇(自我,天气。对的,0)   self.gauge.SetConstraints (lc)      def setPercent(自我,%):   self.gauge.setPercent(百分比)      def setBackgroundColour(自我,颜色):   self.gauge.SetBackgroundColour(颜色)      def setBarColour(自我,颜色):   self.gauge。颜色=颜色      def getBarColour(自我):   返回self.gauge.color      def setStep(自我,步骤):   self.gauge.step=一步      def makeStep(自我):   如果self.gauge.percent>=1。:返回   self.gauge.percent +=self.gauge.step   如果self.gauge.percent> 1。:   self.gauge.percent=1。   ,()   self.Update ()      类指标(wx.Control):   default_color=奥躺?“红色”,“蓝色”#“绿色”“红”“黄”   def __init__(自我、父母身份证、pos=wx.DefaultPosition大?wx.DefaultSize):   wx.Control。__init__(自我、父id、pos、大小)   self.SetBackgroundColour (wx.WHITE)   自我。颜色=Gauge.default_color   自我。%=0.0   自我。一步=0.0   wx。self.OnPaint EVT_PAINT(自我)      def OnPaint(自我,evt):   dc=wx.PaintDC(自我)   dc.Clear ()   dc.BeginDrawing ()      直流。SetPen (wx.Pen (self.color 0))   直流。SetBrush (wx.Brush (self.color))   w h=self.GetSizeTuple ()   dc.DrawRectangleRect ((0, 0, w * self.percent, h))   percentStr=" % d % % % int (self.percent * 100)   tx,泰=fancytext。getExtent (percentStr特区)   直流。DrawText (percentStr w/2-tx/2 h/2-ty/2)      dc.EndDrawing ()      def setPercent(自我,%):   如果% & lt; 0或百分比的在1。   返回   自我。%=%   def改变颜色(自我、颜色):   自我。颜色=颜色   def色鬼(自我):   返回self.color      def setStep(自我,步骤):   self.step=一步      def makeStep(自我):   如果self.percent>=1。:返回   self.percent +=self.step   如果self.percent> 1。:   self.percent=1。   self.gauge.Refresh ()   self.gauge.Update ()

wxPython实现带颜色的进度条