在面板中使用wxPython SpeedMeter

时间:2018-07-02 17:29:20

标签: wxpython

好的。  因此,我花了大约30分钟的时间来尝试解决此问题,并正确设置代码格式。我得到-1。那没有帮助
我从wxDemo代码开始,将其复制到PyCharm中的新项目中,并且仅在框架中有效。 然后,我将SpeedMeter代码复制到一个使用Frame和许多面板的现有项目中。 我只能显示一个小的黑色矩形。有人可以建议我尝试显示全尺寸仪表吗?

无法使SpeedMeter在面板中工作

import wx  
import wx.lib.inspection  
import wx.lib.buttons  
from math import pi  
try:  
    from agw import speedmeter as SM  
except ImportError:  
    import wx.lib.agw.speedmeter as SM  
class PanelOne(wx.Panel):  
    def __init__(self, parent):  
        wx.Panel.__init__(self, parent=parent)  
        panel = wx.Panel(self, wx.ID_ANY)  
        sizer = wx.FlexGridSizer(1, 1, 5, 5) # FlexGridSizer(rows, cols, vgap, hgap)  
        panel1 = wx.Panel(panel, wx.ID_ANY, style=wx.SUNKEN_BORDER)  

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

    speed.SetAngleRange(-pi/6, 7*pi/6)  
    intervals = range(0, 201, 20)  
    speed.SetIntervals(intervals)  
    colours = [wx.BLACK]*10  
    speed.SetIntervalColours(colours)  
    ticks = [str(interval) for interval in intervals]  
    speed.SetTicks(ticks)  
    speed.SetTicksColour(wx.WHITE)  
    speed.SetNumberOfSecondaryTicks(5)  
    speed.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))  
    speed.SetMiddleText("Volts")  
    speed.SetMiddleTextColour(wx.WHITE)  
    speed.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))  
    speed.SetHandColour(wx.Colour(255, 50, 0))  
    speed.DrawExternalArc(False)  
    speed.SetSpeedValue(44)  
    sizer.Add(panel1, 1, wx.EXPAND)  
    panel.SetSizer(sizer)  

    mainSizer = wx.BoxSizer(wx.VERTICAL)  
    mainSizer.Add(panel, 1, wx.EXPAND)  
    self.SetSizer(mainSizer)  
    mainSizer.Layout()   

class MyApp(wx.App):  
    '''Application class is only run if it is __main__. '''  
    def OnInit(self):  
        title = "test code"  
        pos = (400,400)  
        size =(570,280)  
        self.frame = class_myframe(title, pos , size)  
        self.frame.Show(True)  
        self.SetTopWindow(self.frame)  
        return True  

class class_myframe(wx.Frame):  
    def __init__(self,title,pos,size):  
        wx.Frame.__init__(self, None, wx.ID_ANY, title, pos, size)  
        self.panel_one = PanelOne(self)  
        self.boxsizer = wx.BoxSizer(wx.VERTICAL)  
        self.boxsizer.Add(self.panel_one, 1, wx.EXPAND)  
        self.SetSizer(self.boxsizer)  
        self.boxsizer.Fit(self) # This changes the size of the frame to Fit all the widgets  
        self.boxsizer.Layout()  

app = MyApp(False)   
app.MainLoop()  

1 个答案:

答案 0 :(得分:0)

您的代码有点复杂,无法实现明显的目标,但是我不知道您打算如何处理它。希望下面修改后的代码有足够的注释来描述正在发生的事情,以提供帮助。

import wx
from math import pi
try:
    from agw import speedmeter as SM
except ImportError:
    import wx.lib.agw.speedmeter as SM
class PanelOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        panel = wx.Panel(self, wx.ID_ANY)
        panel1 = wx.Panel(panel, wx.ID_ANY, style=wx.SUNKEN_BORDER)

        self.speed = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (300,300), mousestyle=1)

        self.speed.SetAngleRange(-pi/6, 7*pi/6)
        intervals = range(0, 201, 20)
        self.speed.SetIntervals(intervals)
        colours = [wx.BLACK]*10
        self.speed.SetIntervalColours(colours)
        ticks = [str(interval) for interval in intervals]
        self.speed.SetTicks(ticks)
        self.speed.SetTicksColour(wx.WHITE)
        self.speed.SetNumberOfSecondaryTicks(5)
        self.speed.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed.SetMiddleText("Volts")
        self.speed.SetMiddleTextColour(wx.WHITE)
        self.speed.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed.SetHandColour(wx.Colour(255, 50, 0))
        self.speed.DrawExternalArc(False)
        self.speed.SetSpeedValue(44)
    #Bind mouse events
        self.speed.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)
        self.speed.SetToolTip(wx.ToolTip("Drag the speed dial to change the speed!"))
    #Define the control slider
        self.slider = wx.Slider(panel1, -1, 44, 0, 200,
                           style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS )
        self.slider.SetTickFreq(5)
    #Bind the slider
        self.slider.Bind(wx.EVT_SCROLL, self.OnSliderScroll)
        self.slider.SetToolTip(wx.ToolTip("Drag The Slider To Change The Speed!"))

    #Create required sizers
        vsizer1 = wx.BoxSizer(wx.VERTICAL)
        hsizer1 = wx.BoxSizer(wx.HORIZONTAL)

        hsizer1.Add(self.slider, 1, wx.EXPAND)
        vsizer1.Add(self.speed, 0, wx.EXPAND)
        vsizer1.Add(hsizer1, 0, wx.EXPAND)
    #Set the panel1 sizer
        panel1.SetSizer(vsizer1)
    #Fit contents
        panel1.Fit()
    #Implement the main sizer
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(mainSizer)
        mainSizer.Layout()

    def OnSliderScroll(self, event):
        slider = event.GetEventObject()
        self.speed.SetSpeedValue(slider.GetValue())
        event.Skip()

    def OnMouse(self, event):
        speed = event.GetEventObject()
        self.slider.SetValue(speed.GetSpeedValue())
        event.Skip()


class MyApp(wx.App):
    '''Application class is only run if it is __main__. '''
    def OnInit(self):
        title = "test code"
        pos = (400,400)
        size =(570,280)
        self.frame = class_myframe(title, pos , size)
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        return True

class class_myframe(wx.Frame):
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self, None, wx.ID_ANY, title, pos, size)
        self.panel_one = PanelOne(self)
        self.boxsizer = wx.BoxSizer(wx.VERTICAL)
        self.boxsizer.Add(self.panel_one, 1, wx.EXPAND)
        self.SetSizer(self.boxsizer)
        self.boxsizer.Fit(self) # This changes the size of the frame to Fit all the widgets
        self.boxsizer.Layout()

app = MyApp(False)
app.MainLoop() 

我已将控制滑块和鼠标移动添加到原始代码中,主要是出于我自己的利益,因为我以前没有使用过SpeedMeter