Ok, I realize that’s not a very clear title, so hopefully this can clarify it a bit better (and if anybody can come up with a clearer name for it, I’d appreciate the help!).
I am creating a points tracker for my kindergarten class. What I see in my mind is a bar with a 0 at the bottom and a 100 at the top, and a color gradient points bar that scans up and down from 0 to 100. When they hit the touch screen, the bar freezes and their team gets x points, where x is the number the bar freezes on.
Right now, I’m just working on the function which moves the bar (haven’t even got the freezing/interrupting put in yet). I’ve put this together, but it feels really inelegant and ugly.
def score_inc():
x = 0
while x < 100:
x += 1
time.sleep(0.01)
while x > 100:
x -= 1
time.sleep(0.01)
Not to mention, I’m reasonably confident this code would block operation, meaning I couldn’t display/update the graphics.
This is my first foray into GUI programming, so I’m taking it cautiously and trying to write a GUI-less backend before I write the graphical frontend, is that the wrong route? Is there a smoother, less ugly way to implement something like this using a function which is ‘GUI aware’, for lack of a better term?
In wxPython, you’d probably want to use a wx.Slider, although I don’t think it supports the colors you’re looking for. However, you might be able to tie the slider events to a wx.GCDC or wx.PaintDC that can do it for you. I’d ask on the wxPython user’s group for other suggestions.
You are right about time.sleep() blocking the GUI’s mainloop. Your GUI would not respond if you did it that way. Instead, you should use a wx.Timer. Here’s a tutorial on that. You could put the time.sleep() into a thread, but I think that’s overkill and will make it needlessly complicated.