这有点可怕,因为你把所有的东西都抵消了90度,所以你必须对此作出解释。
什么都没有:import wx
import math
class TestArc(wx.Panel):
def __init__(self, *args, **kwargs):
super(TestArc, self).__init__(*args, **kwargs)
self.lineWidth = 0
self.min = -90
self.max = 360
self._value = 0
self.setText = ' -'
# self.font = someFont()
self.position = wx.Rect() # self.position.Set(x, y, width, height)
self.startPoint = math.radians(0)
self.endPoint = math.radians(0)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def setRange(self, min, max):
self.min = min
self.max = max
if self._value < self.min:
self._value = self.min
elif self._value > self.max:
self._value = self.max
self.Refresh()
def setMinimun(self, min):
self.setRange(min, self.max)
def setMaximun(self, max):
self.setRange(self.min, max)
def setValue(self, val):
if self._value != val:
if val < self.min:
self._value = self.min
elif val > self.max:
self._value = self.max
else:
self._value = val
self.Refresh()
def setLineWidth(self, lineWidth):
self.lineWidth = lineWidth
def setPosition(self, x, y, width, height):
self.position = wx.Rect(x, y, width, height)
def OnPaint(self, event=None):
dc = wx.PaintDC(self)
gc = self.MakeGC(dc)
self.Draw(gc)
def MakeGC(self, dc):
try:
if False:
gcr = wx.GraphicsRenderer.GetCairoRenderer
gc = gcr() and gcr().CreateContext(dc)
if gc is None:
wx.MessageBox("Unable to create Cairo Context.", "Oops")
gc = wx.GraphicsContext.Create(dc)
else:
gc = wx.GraphicsContext.Create(dc)
except NotImplementedError:
dc.DrawText("This build of wxPython does not support the wx.GraphicsContext "
"family of classes.",
25, 25)
return None
return gc
def Draw(self, gc):
#middle progressbar line
radStart = math.radians(90)
radEnd = math.radians(0)
path = gc.CreatePath()
path.AddArc(80, 80, 50, radStart, radEnd, True)
pen = wx.Pen('#000000', 4)
pen.SetCap(wx.CAP_BUTT)
gc.SetPen(pen)
gc.SetBrush(wx.Brush('#000000', wx.TRANSPARENT))
gc.DrawPath(path)
#progress bar
start = math.radians(90)
#r = math.radians(270)
arcStep = 270 / (self.max - self.min) * self._value
end = math.radians(arcStep)
path = gc.CreatePath()
path.AddArc(80, 80, 50, start, end)
pen = wx.Pen('#CC7F32', 15)
pen.SetCap(wx.CAP_BUTT)
gc.SetPen(pen)
gc.SetBrush(wx.Brush('#000000', wx.TRANSPARENT))
gc.DrawPath(path)
class bandom(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(800, 600))
self.gauge = TestArc(self)
self.gauge.SetFocus()
self.gauge.setMinimun(90)
self.gauge.setMaximun(360)
self.gauge.setValue(90)
# timer for testing progressbar
self.val = 90
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(100)
def OnTimer(self, evt):
self.val += 2.7
self.gauge.setValue(self.val)
if self.val >= 360:
self.val = 90
if __name__ == '__main__':
app = wx.App()
frame = bandom(None, -1, 'Window title here')
frame.Show(True)
app.MainLoop()
为了方便起见,我把它们都塞进了一个文件。
编辑:
将最大值更改为360并使用270计算圆弧中的步长的原因是,您选择使用3/4圆作为量规,并且使用弧度计算量规增量的数量。你可以用度数或弧度,但不管怎样,它都会被计算在0到270度之间,或者0到3π/2之间。最初你说你需要一个0到100之间的刻度,最简单的安排方法是将仪表计数器增加/减少2.7而不是1,即270点到行程/100(您需要的刻度)。从0点开始测量比从90点开始更容易,但我假设您这样做是出于练习或出于审美考虑。
您关于需要240个增量的最后一个注释与原始问题和其他注释冲突,但可以通过将量规计数器增加1.125(270/240)而不是1或2.7来实现
编辑2:
在我的机器上运行这个代码,我没有“闪烁”。这可能取决于您的机器速度,如果您更改传递给wx.Timer的值(当前为每秒100毫秒或10次),则可能会有所改善。请记住,这段代码本质上是通过使用计时器来显示任务的进度,实际上,您将根据实际进程的进度而不是计时器将值传递到仪表中。在