Python_SpeedGauge针没有更新滑块值

时间:2014-08-20 17:09:34

标签: python wxpython

我创建了一个speedmeeter(模拟仪表),其中针应该取相关滑块的值。我运行代码,针停留在滑块的初始值(此处为50) 有人可以帮忙吗?感谢

import wx
import os
import wx.lib.agw.speedmeter as SM
import sys
import math



class MyFrame(wx.Frame):

    def __init__(self,parent):
        # creation du panneau
        wx.Frame.__init__(self,parent,-1,"test")



        speed = SM.SpeedMeter(self, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS)

        # Set The Region Of Existence Of SpeedMeter 
        speed.SetAngleRange(-math.pi/6, 11*math.pi/6)

        # SpeedMeter In Sectors
        intervals = range(0, 201, 20)
        speed.SetIntervals(intervals)

        # Assign The Same Colours To All Sectors 
        # Usually This Is Black
        colours = [wx.BLACK]*10
        speed.SetIntervalColours(colours)

        # Assign The Ticks
        ticks = [str(interval) for interval in intervals]
        speed.SetTicks(ticks)
        # Set The Ticks/Tick Markers Colour
        speed.SetTicksColour(wx.WHITE)
        # We Want To Draw 5 Secondary Ticks Between The Principal Ticks
        speed.SetNumberOfSecondaryTicks(5)

        # Set The Font For The Ticks Markers
        speed.SetTicksFont(wx.Font(7, wx.SWISS, wx.NORMAL, wx.NORMAL))

        # Set The Text In The Center Of SpeedMeter
        speed.SetMiddleText("Km/h")
        # Assign The Colour To The Center Text
        speed.SetMiddleTextColour(wx.BLACK)
        # Assign A Font To The Center Text
        speed.SetMiddleTextFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))

        # Set The Colour For The Hand Indicator
        speed.SetHandColour(wx.Colour(255, 50, 0))

        # Do Not Draw The External (CONTAINER) Arc
        speed.DrawExternalArc(False)

        #set gauge to initial slider position
        #self.pos=self.slider.GetValue()

        #creation ofslider

        self.slider=wx.Slider(speed,-1,50,0,150,pos=(15,415),size=  (350,-1),style=wx.SL_AUTOTICKS|wx.SL_LABELS)
        self.slider.SetTickFreq(5,1)

        #value of needle

        val=self.slider.Bind(wx.EVT_SLIDER,self.sliderUpdate)

    def sliderUpdate(self, event):
        #get slider position
        val=self.slider.Bind(wx.EVT_SLIDER,self.sliderUpdate)
        self.pos=self.slider.GetValue()


APP=wx.App(0)

frame=MyFrame(None)
APP.SetTopWindow(frame)
frame.Show()
APP.MainLoop() 

1 个答案:

答案 0 :(得分:0)

您没有设置事件处理程序sliderUpdate来设置车速表的值。您需要使速度表变量speed成为类属性。我对代码进行了必要的更改。

class MyFrame(wx.Frame):

    def __init__(self,parent):
        # creation du panneau
        wx.Frame.__init__(self,parent,-1,"test")

        self.speed = SM.SpeedMeter(self, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS)

        # Set The Region Of Existence Of SpeedMeter 
        self.speed.SetAngleRange(-math.pi/6, 11*math.pi/6)

        # SpeedMeter In Sectors
        intervals = range(0, 201, 20)
        self.speed.SetIntervals(intervals)

        # Assign The Same Colours To All Sectors 
        # Usually This Is Black
        colours = [wx.BLACK]*10
        self.speed.SetIntervalColours(colours)

        # Assign The Ticks
        ticks = [str(interval) for interval in intervals]
        self.speed.SetTicks(ticks)
        # Set The Ticks/Tick Markers Colour
        self.speed.SetTicksColour(wx.WHITE)
        # We Want To Draw 5 Secondary Ticks Between The Principal Ticks
        self.speed.SetNumberOfSecondaryTicks(5)

        # Set The Font For The Ticks Markers
        self.speed.SetTicksFont(wx.Font(7, wx.SWISS, wx.NORMAL, wx.NORMAL))

        # Set The Text In The Center Of SpeedMeter
        self.speed.SetMiddleText("Km/h")
        # Assign The Colour To The Center Text
        self.speed.SetMiddleTextColour(wx.BLACK)
        # Assign A Font To The Center Text
        self.speed.SetMiddleTextFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))

        # Set The Colour For The Hand Indicator
        self.speed.SetHandColour(wx.Colour(255, 50, 0))

        # Do Not Draw The External (CONTAINER) Arc
        self.speed.DrawExternalArc(False)

        #set gauge to initial slider position
        #self.pos=self.slider.GetValue()

        #creation ofslider
        self.slider=wx.Slider(self.speed,-1,50,0,150,pos=(15,415),size=  (350,-1),style=wx.SL_AUTOTICKS|wx.SL_LABELS)
        self.slider.SetTickFreq(5,1)

        #binding the event of slider change
        self.slider.Bind(wx.EVT_SLIDER,self.sliderUpdate)

    def sliderUpdate(self, event):
        #get slider position and set it on speedometer
        self.speed.SetSpeedValue(self.slider.GetValue())