首先我是python的新手,所以请不要判断我代码中的混乱:)。我试图制作循环进度条,但我遇到了一些问题,无法找到它导致的问题。问题是 setRange(0,100)或setMmaximum(100)如果我setValue(100)我无法填充所有圆圈。第二个问题是我的进度条向后运行。请有人解释我做错了什么?
这是我到目前为止所做的:
main.py文件
import wx
from src.arc import TestArc
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(0)
#self.gauge.setMaximun(100)
self.gauge.setRange(0, 100)
self.gauge.setValue(10)
# timer for testing progressbar
# self.timer = wx.Timer(self)
# self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
# self.timer.Start(100)
# self.val = 0
# def OnTimer(self, evt):
# self.val += 1
# self.gauge.setValue(self.val)
#
# if self.val >= 100:
# self.val = 0
# #print(self.val)
class MyApp(wx.App):
def OnInit(self):
frame = bandom(None, -1, 'Window title here')
frame.Show(True)
self.SetTopWindow(frame)
return True
def main():
app = MyApp(0)
app.MainLoop()
if __name__ == '__main__':
main()
和arc.py文件
class TestArc(wx.Panel):
def __init__(self, *args, **kwargs):
super(TestArc, self).__init__(*args, **kwargs)
self.lineWidth = 0
self.min = 0
self.max = 100
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.max < self.min:
self.max, self.min = self.min, self.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()
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)
答案 0 :(得分:1)
这有点令人毛骨悚然,因为你已将所有东西偏移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次,则可能会提高。请记住,这段代码本质上假装通过使用计时器来显示任务的进度,实际上你会将值传递给计量器而不是基于计时器而是基于实际进程的进度。