wxPython:如何删除滑块并返回使用函数CpuData填充自定义量表

时间:2014-02-22 04:43:51

标签: python wxpython

我是Python的新手,并且一直在线阅读一些wxPython教程,并想知道如何用实际数据填充我的自定义cpu量表!

目前,自定义量表从滑块小部件中获取其值/数据,用户必须手动向上或向下拖动。

我编写了一个名为CpuData()的简单函数,它基本上会返回0到100之间的虚拟CPU值。

现在我的问题是如何消除Slider并通过循环遍历CpuData()函数自动填充我的仪表。

抱歉新手问题!任何帮助将不胜感激。

Current Custom Gauge Layout

import wx
import random


def CpuData():      # Will return a random CPU value
cData = random.randint(0,100)
print cData

CpuData()

class CPU(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(80, 110))

        self.parent = parent

        self.SetBackgroundColour('#000000')


        self.Bind(wx.EVT_PAINT, self.OnPaint)


    def OnPaint(self, event):

        dc = wx.PaintDC(self)

        dc.SetDeviceOrigin(0, 100)
        dc.SetAxisOrientation(True, True)

        pos = self.parent.GetParent().GetParent().sel
        rect = pos / 5

        for i in range(1, 21):
            if i > rect:
                dc.SetBrush(wx.Brush('#075100'))
                dc.DrawRectangle(10, i*4, 30, 5)
                dc.DrawRectangle(41, i*4, 30, 5)
            else:
                dc.SetBrush(wx.Brush('#36ff27'))
                dc.DrawRectangle(10, i*4, 30, 5)
                dc.DrawRectangle(41, i*4, 30, 5)


class CPUWidget(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(190, 140))

        self.sel = 0

        panel = wx.Panel(self, -1)
        centerPanel = wx.Panel(panel, -1)

        self.cpu = CPU(centerPanel, -1)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.slider = wx.Slider(panel, -1, self.sel, 0, 100, (-1, -1), (25, 90), 
    wx.VERTICAL | wx.SL_LABELS | wx.SL_INVERSE)
        self.slider.SetFocus()

        hbox.Add(centerPanel, 0,  wx.LEFT | wx.TOP, 20)
        hbox.Add(self.slider, 0, wx.LEFT | wx.TOP, 23)


        self.Bind(wx.EVT_SCROLL, self.OnScroll)

        panel.SetSizer(hbox)

        self.Centre()
        self.Show(True)


    def OnScroll(self, event):
        self.sel = event.GetInt()
        self.cpu.Refresh()

app = wx.App()
CPUWidget(None, -1, 'cpu')
app.MainLoop()

1 个答案:

答案 0 :(得分:1)

从概念上讲,此代码通过调用触发自定义事件的函数的计时器来生成新值并更新CPU图。

计时器由wxpython提供,但是任何计时功能都可以工作(例如:运行睡眠功能的线程)。

在此特定示例中,不需要自定义事件,因为计时器由GUI本身提供。尽管如此,使用事件是一个很好的做法,因为它允许更抽象的实现(允许更容易的重构)并消除非GUI线程生成数据时的线程问题。

import wx
import wx.lib.newevent

import random

CpuEvent, EVT_CPU_EVENT = wx.lib.newevent.NewEvent()

class CPU(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(80, 110))

        self.value  = 100
        self.parent = parent

        self.SetBackgroundColour('#ffffff')

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(EVT_CPU_EVENT, self.do_value)

    def do_value(self, event):
        self.value = event.value
        self.Refresh()

    def OnPaint(self, event):

        dc = wx.PaintDC(self)

        dc.SetDeviceOrigin(0, 100)
        dc.SetAxisOrientation(True, True)

        pos = self.value
        rect = pos / 5

        for i in range(1, 21):
            if i > rect:
                dc.SetBrush(wx.Brush('#075100'))
                dc.DrawRectangle(10, i*4, 30, 5)
                dc.DrawRectangle(41, i*4, 30, 5)
            else:
                dc.SetBrush(wx.Brush('#36ff27'))
                dc.DrawRectangle(10, i*4, 30, 5)
                dc.DrawRectangle(41, i*4, 30, 5)


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

        self.cpu = CPU(self, -1)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.do_CpuData)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.cpu, 1, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 5)

        self.SetSizer(hbox)
        hbox.Fit(self)
        self.Centre()
        self.Show(True)
        self.timer.Start(1000)

    def do_CpuData(self, event = None):
        cData = random.randint(0, 100)
        event = CpuEvent(value = cData)
        wx.PostEvent(self.cpu, event)

app = wx.App(redirect = False)
CPUWidget(None, -1, 'cpu')
app.MainLoop()