My question is: i made a program that counts up and up continuously in the background but i want it to print what number it is up to when you press a certain button such as ENTER. I have tried using raw_inputs() but all that happens is it keeps waiting every time for an input rather then just continuing counting like it is meant to. Any help would be greatly appreciated.

Recommended Answers

All 6 Replies

If you are using Windows you can do this:

# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
from msvcrt import getch

print "press a char key (escape key to exit)"

while True:
    z = getch()
    # escape key to exit
    if ord(z) == 27:
    print z,

There is also a way to do it with Tkinter running in the background.

That dosent really solve my problem

# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
from msvcrt import getch

print "press a char key (escape key to exit)"
while True:
    z = getch()
    print ord(z)
    # escape key to exit
    if ord(z) == 27:
    print z,x

what i want is x to keep counting wether a button has been pressed or not and when a button is pressed it shows how high x is. for example the first time you press enter x might print as 12 the next at 124 and so on but inbetween buttons being pressed nothing is printed

Well you could just use a Thread object to do the counting for you and then in your main program everytime the user enters a key, you just call a function of the Thread object counting that returns it's current value.

The example below might help.

# counterTest.py, a1eio

import threading

# create a thread object that will do the counting in a separate thread
class counter(threading.Thread):
    def __init__(self, value, increment):
        threading.Thread.__init__(self) # init the thread
        self.value = value # initial value
        self.increment = increment # amount to increment
        self.alive = False # controls the while loop in the run command

    def run(self): # this is the main function that will run in a separate thread
        self.alive = True
        while self.alive:
            self.value += self.increment # do the counting

    def peek(self): # return the current value
        return self.value

    def finish(self): # close the thread, return final value
        self.alive = False # stop the while loop in 'run'
        return self.value # return value

def Main():
    # create separate instances of the counter
    counterA = counter(1, 1) #initial value, increment
    counterB = counter(1, 2) #initial value, increment
    counterC = counter(1, 3) #initial value, increment

    # start each counter

    print "Enter A, B, or C to view counters\nEnter Q to quit"
    while True:
        Input = raw_input("> ").upper() # get input

        if Input == 'A':
            print 'CounterA: ', counterA.peek() # print counterA's value
        elif Input == 'B':
            print 'CounterB: ', counterB.peek() # print counterB's value
        elif Input == 'C':
            print 'CounterC: ', counterC.peek() # print counterC's value
        elif Input == 'Q': # if user entered q or Q, program quits
            # call the function that finishes each counter and print the returned value
            print 'CounterA: ', counterA.finish()
            print 'CounterB: ', counterB.finish()
            print 'CounterC: ', counterC.finish()
            return # exit the Main function


if __name__ == '__main__':


Enter A, B, or C to view counters
Enter Q to quit
> a
CounterA:  428610
> b
CounterB:  1359943
> c
CounterC:  4064077
> q
CounterA:  1841495
CounterB:  2832249
CounterC:  5452621
commented: Very nice solution +8

That is exactly what i was looking for!
great ideas and it works really well.
Problem Solved!

Very nice example of threading a1eio!

Could you do all of us a favor and contribute that to the Python code snippets? It would be much appreciated!

My appologies, but just a hint to stay in the Python code guidelines, in the snippet could you start your class with an upper case letter and your function Main() with a lower case letter? That would make it perfect!

Yea not a problem i'll put it there now.

And I will look up the python code guidlines thing (i have heard of it before to be honest) i'm just lazy hehe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.