Why does this code crash python?

2006-11-11 Thread Mythmon
I am trying to make a program that will basically simulate a chess
clock in python. To do this I have two threads running, one that
updates the currently running clock, and one that watches for a
keypress. I am using the effbot Console module, and that is where I get
the events for the keypresses. But when I press space it crashes
shortly after. The program stops, the Console closes, and windows says
that the program pythonw.exe has had an unexpected reason to shut down.
I am using python2.5.

If there is a simpler way to do this ( I tried without threads but had
some problems with the Console module giving me input right and still
letting the timer display count down ) I would like to know about it. I
am not a very good programmer, and don't have much experience with
python

note: A chess clock is two timers that count down from a set time, and
when you start one of the timer the other stops.

Code below:

import time
import Console
from threading import Thread

c = Console.getconsole()

turn = 0

class timer (Thread):
def run ( self ):
global turn
oldTime = [time.time(), time.time()]
timeLeft = [260, 260]

go = True
while go:
newTime = time.time()
timeLeft[turn] -= newTime - oldTime[turn]
oldTime[turn] = newTime
minutes = [str(int(timeLeft[0]//60)),
str(int(timeLeft[1]//60))]
seconds = [str(timeLeft[0]%60)[0:5],
str(timeLeft[1]%60)[0:5]]

if float(seconds[0]) < 10: seconds[0] = '0' +
seconds[0][:-1]
if float(seconds[1]) < 10: seconds[1] = '0' +
seconds[1][:-1]

c.text(3,3,minutes[0] + ':' + seconds[0])
c.text(12,3,minutes[1] + ':' + seconds[1])

time.sleep(.1)

class eventMonitor (Thread):
def run ( self ):
global turn
go = True
while go:
event = c.peek()
if event != None:
c.get()
if event.type == 'KeyPress':
if event.keycode == 32:
if turn == 1: turn = 0
if turn == 0: turn = 1
c.text(10,20,'1')

timer().start()
eventMonitor().start()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does this code crash python?

2006-11-11 Thread Mythmon
On Nov 11, 11:28 am, Irmen de Jong <[EMAIL PROTECTED]> wrote:
> You're creating a console object "c" and use it in both threads.
>
> Try creating the console object for each thread by itself,
> i.e. remove the global "c = Console.getconsole()" and replace
> it by a getconsole() inside each thread class.
>
> I don't have the Console module so I don't know if it fixes things
> for you, but give it a try :)

Well, I tried that, and it did something. It made it so the space bar
switched the clock once, but not a second time. And it still crashed,
but not always at the same time, sometimes it would do it the second
time I hit the space bar, sometimes before I hit it the first time, but
always when i did something that would generate a Console event (moving
the mouse or something). So, I thought it had something to do with
Console not working well with threads, so I took it completely out of
threads and had just the event checking loop run by itself, and it
still crashed. So apparently I am using the Console events wrong. I am
going to try and find some examples online on how to watch for events
with it, but if worse comes to worse I can try and use keyboard hooks,
I've looked at those before, and they might work better.

--Mythmon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does this code crash python?

2006-11-11 Thread Mythmon


On Nov 11, 3:23 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> I have no knowledge of Console, but under Windows I was able to hack
> this (see bottom of post) together using only one non-portable library
> -- msvcrt. Oh, and an ActiveState 2.4 build. I'm not touching 2.5 until
> a lot of 3rd party stuff is rebuilt and installers are available.
>

And here I am thinking this was going to be an easy project. I haven't
had a chance to work on this since my last message, but that example
should be a good thing, since now i have an example of how threads
/should/ be done. I was pretty much just guessing so far.

>
> > newTime = time.time()
> > timeLeft[turn] -= newTime - oldTime[turn]I'd duplicated 
> > this form, but it is wrong... when the clock swaps it
> will immediately decrement the current clock by the difference since it
> was last updated!
>
> > if float(seconds[0]) < 10: seconds[0] = '0' +
> > seconds[0][:-1]
> > if float(seconds[1]) < 10: seconds[1] = '0' +
> > seconds[1][:-1]I've not handled leading 0s on the seconds field (I 
> > tried, but can't
> figure out how to specify zero-filled field width for floats -- works
> for integers)
>

I ran into the same problem, thats why I did it the way I did, instead
of continuing to wrestle with python's string formatting.

> > go = True
> > while go:Why define "go" when it never changes

Whenever I make infinite loops, I put in a control variable so I could
stop it if I want, which I was planning on doing, when the time runs
out or something like that.


>
> Note that I lock access to timeLeft and side to ensure they don't
> change in the middle of access; may not have been critical, but why risk
> it (and if the code gets expanded so the changes take more time -- or
> multiple look-ups using side).
>
> getch() doesn't seem to return a value for , so I used
> 
>
> Uh, it also updates the time with an invalid negative before it
> exits 

Now to see if I can get this to work for my self with an example. This
is more a learning project than something that has to be done.

-- 
http://mail.python.org/mailman/listinfo/python-list