Why does this code crash python?

Mythmon at gmail.com Mythmon at gmail.com
Sat Nov 11 14:17:06 EST 2006


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()




More information about the Python-list mailing list