如何在同一帧上设置2个速度表

时间:2014-08-26 23:05:56

标签: python wxpython

我有以下代码适用于一个速度表。通过修改滑块,针将自身调整到速度表上的相应值。我在同一帧上创建了第二个速度表和滑块。在运行代码时,我看到只有两个滑块和两个小点出现 - 没有速度计。如何让车速表显示?

提前致谢。

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


class MyFrame(wx.Frame):

    def __init__(self,parent,id,title):
        # creation frame
        wx.Frame.__init__(self,parent,-1,"test2",size=(1200,1200))

        self.speed = SM.SpeedMeter(self, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS,pos=(15,300),size=(35,-1))

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

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

        # Assign The Same Colours To All Sectors 

        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.YELLOW)
        # 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("L")
        # Assign The Colour To The Center Text
        self.speed.SetMiddleTextColour(wx.WHITE)
        # Assign A Font To The Center Text
        self.speed.SetMiddleTextFont(wx.Font(28, wx.SWISS, wx.NORMAL, wx.BOLD))

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

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



        #creation of slider 1
        self.slider=wx.Slider(self,-1,50,0,200,pos=(15,100),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)


        ##############################
        # Second speedmeter


        self.speed2 = SM2.SpeedMeter(self, agwStyle=SM2.SM_DRAW_HAND|SM2.SM_DRAW_SECTORS|SM2.SM_DRAW_MIDDLE_TEXT|SM2.SM_DRAW_SECONDARY_TICKS,pos=(15,30),size=(35,-1))

        # Set The Region Of Existence Of SpeedMeter 2
        self.speed2.SetAngleRange(-math.pi/6, 7*math.pi/6)

        # SpeedMeter In Sectors
        intervals2 = range(0, 201, 20)
        self.speed2.SetIntervals(intervals2)

        # Assign The Same Colours To All Sectors 

        colours2 = [wx.BLACK]*10
        self.speed2.SetIntervalColours(colours2)

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

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

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

        # Set The Colour For The Hand Indicator
        self.speed2.SetHandColour(wx.Colour(255, 250, 20))

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

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

        #creation ofslider

        self.slider2=wx.Slider(self,2,50,0,200,pos=(15,415),size=  (350,-1),style=wx.SL_AUTOTICKS|wx.SL_LABELS)
        self.slider2.SetTickFreq(5,1)

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


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

    def sliderUpdate2(self, event):
        #get slider position and set it on speedometer
        self.speed2.SetSpeedValue(self.slider2.GetValue())




if __name__=='__main__' :        
    APP=wx.App(0)

    frame=MyFrame(None,-1,'Test')
    APP.SetTopWindow(frame)
    frame.Show()
    APP.MainLoop() 

1 个答案:

答案 0 :(得分:1)

当您实例化速度表时,应使用与当前速度表不同的尺寸 试试size=(100,100),你会看到你的车速表出现了。

在任何情况下,您可能希望使用sizer来定位小部件并使其与框架一起展开或压缩,而不是使用possize的静态定位