473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

wxpython: Timer object not working, or just me?

I'd be curious to know if this works any differently on other
computers/platforms or while other things are happening in the
background. I can't tell if it's the Timer object that isn't keep
accurate time (although a test with time.time() seems to show that it
is), or if I'm just messing up my algorithm to fill the progress bar. If
I put in 10 seconds, the progress bar will be fully filled at the end,
but if you put 30, it doesn't. As the number gets higher, the less the
bar gets filled when it's done running. (52 seconds only fills half the
bar at the end!)

Hopefully someone can look at my logic (mostly in the OnTimer method)
and see right away how I'm screwing this up.

Thanks.

-------------------------

import wx
class MyTimer(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
panel = wx.Panel(self)

mainSizer = wx.BoxSizer(wx.VERTICAL)
inputSizer = wx.BoxSizer(wx.HORIZONTAL)

self.minutesPassed = 1
self.progress = wx.Gauge(panel, wx.ID_ANY, 100, size=(300, 20))
self.status = wx.StaticText(panel, wx.ID_ANY, 'Enter a time.')
prompt = wx.StaticText(panel, wx.ID_ANY, 'Time to wait:')
self.input = wx.TextCtrl(panel, wx.ID_ANY, '30', size=(20, 20))
self.start = wx.Button(panel, wx.ID_ANY, 'Start')
self.reset = wx.Button(panel, wx.ID_ANY, 'Reset')
self.reset.Disable()
self.timer = wx.Timer(self)

mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.ALL ^
wx.BOTTOM,
border=10)
mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
border=10)
mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER | wx.BOTTOM,
border=10)
inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.input, flag=wx.ALIGN_CENTER | wx.LEFT |
wx.RIGHT,
border=5)
inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.reset, flag=wx.ALIGN_CENTER)

self.Bind(wx.EVT_TEXT_ENTER, self.OnStart, self.input)
self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Bind(wx.EVT_BUTTON, self.OnReset, self.reset)

panel.SetSizer(mainSizer)
mainSizer.Fit(self)

def OnStart(self, event):
self.time = int(self.input.GetValue())
self.countdown = self.time - 1
self.start.Disable()
self.reset.Enable()
self.input.Disable()
self.status.SetLabel('%s minutes remaining.' % self.time)
self.timer.Start(1000)

def OnReset(self, event):
self.input.Clear()
self.status.SetLabel('Enter a new time.')
self.progress.SetValue(0)
self.input.Enable()
self.minutesPassed = 1

def OnTimer(self, event):
if self.countdown != 0:
#print self.minutesPassed, self.countdown
self.progress.SetValue(self.minutesPassed * (100 / self.time))
self.status.SetLabel('%s minutes remaining.' % self.countdown)
self.minutesPassed += 1
self.countdown -= 1
else:
self.progress.SetValue(self.minutesPassed * (100 / self.time))
self.status.SetLabel('%s minutes have elapsed.' % self.time)
class MyApp(wx.App):

def OnInit(self):
frame = MyTimer()
self.SetTopWindow(frame)
frame.Show()
return True
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()
Jul 26 '06 #1
3 2496
John Salerno wrote:
I'd be curious to know if this works any differently on other
computers/platforms or while other things are happening in the
background. I can't tell if it's the Timer object that isn't keep
accurate time (although a test with time.time() seems to show that it
is), or if I'm just messing up my algorithm to fill the progress bar. If
I put in 10 seconds, the progress bar will be fully filled at the end,
but if you put 30, it doesn't. As the number gets higher, the less the
bar gets filled when it's done running. (52 seconds only fills half the
bar at the end!)

Hopefully someone can look at my logic (mostly in the OnTimer method)
and see right away how I'm screwing this up.

Thanks.

-------------------------

import wx
class MyTimer(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
panel = wx.Panel(self)

mainSizer = wx.BoxSizer(wx.VERTICAL)
inputSizer = wx.BoxSizer(wx.HORIZONTAL)

self.minutesPassed = 1
self.progress = wx.Gauge(panel, wx.ID_ANY, 100, size=(300, 20))
self.status = wx.StaticText(panel, wx.ID_ANY, 'Enter a time.')
prompt = wx.StaticText(panel, wx.ID_ANY, 'Time to wait:')
self.input = wx.TextCtrl(panel, wx.ID_ANY, '30', size=(20, 20))
self.start = wx.Button(panel, wx.ID_ANY, 'Start')
self.reset = wx.Button(panel, wx.ID_ANY, 'Reset')
self.reset.Disable()
self.timer = wx.Timer(self)

mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.ALL ^
wx.BOTTOM,
border=10)
mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
border=10)
mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER | wx.BOTTOM,
border=10)
inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.input, flag=wx.ALIGN_CENTER | wx.LEFT |
wx.RIGHT,
border=5)
inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)
inputSizer.Add(self.reset, flag=wx.ALIGN_CENTER)

self.Bind(wx.EVT_TEXT_ENTER, self.OnStart, self.input)
self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Bind(wx.EVT_BUTTON, self.OnReset, self.reset)

panel.SetSizer(mainSizer)
mainSizer.Fit(self)

def OnStart(self, event):
self.time = int(self.input.GetValue())
self.countdown = self.time - 1
self.start.Disable()
self.reset.Enable()
self.input.Disable()
self.status.SetLabel('%s minutes remaining.' % self.time)
self.timer.Start(1000)

def OnReset(self, event):
self.input.Clear()
self.status.SetLabel('Enter a new time.')
self.progress.SetValue(0)
self.input.Enable()
self.minutesPassed = 1

def OnTimer(self, event):
if self.countdown != 0:
#print self.minutesPassed, self.countdown
self.progress.SetValue(self.minutesPassed * (100 / self.time))
self.status.SetLabel('%s minutes remaining.' % self.countdown)
self.minutesPassed += 1
self.countdown -= 1
else:
self.progress.SetValue(self.minutesPassed * (100 / self.time))
self.status.SetLabel('%s minutes have elapsed.' % self.time)
class MyApp(wx.App):

def OnInit(self):
frame = MyTimer()
self.SetTopWindow(frame)
frame.Show()
return True
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()
Integer arithmetic. Try changing the "100" to "100.0" and you'll see it
works flawlessly.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 26 '06 #2
Steve Holden wrote:
Integer arithmetic. Try changing the "100" to "100.0" and you'll see it
works flawlessly.
oh my god, i don't deserve to use python!

but thanks so much! :)
Jul 26 '06 #3
John Salerno wrote:
Steve Holden wrote:

>>Integer arithmetic. Try changing the "100" to "100.0" and you'll see it
works flawlessly.


oh my god, i don't deserve to use python!

but thanks so much! :)
;-)
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jblazi | last post by:
I am having this problem: I use Python for my teaching and right now we are trying to implement some sort of very simple graphical game the pupils can play across the Internet. For this, we use...
2
by: Taki Jeden | last post by:
Hi Anybody used wxPython with twisted? I started putting together a Twisted-based app with wx GUI, and the widgets just don't work - some controls do not show up etc. - at least on my system....
8
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
2
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming...
8
by: Jan Danielsson | last post by:
Hello all, I wanted to plot some statistics, so I wrote a simple wxPython class to do it. Then I realized that I would like to draw bar graphs, so I added that too. Since I'm a complete...
0
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this...
0
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this...
2
by: whatazor | last post by:
Hi all, I migrate some code from tkinter to wxpython. I need the equivalent Tkinter method Tkinter.Tk.after in wxPython, but I'm not able to find it. It exist or there are other trick to emulate...
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.