Question

I am writing a program, where my goal is to display information, whenever user press a key from laptop.

Here's the program:

# Python program
# to display the fruit information
# User will press a key, and based on the key pressed, the fruit information will be diplayed
# If user presses a wrong key, then, a message will be displayed asking him to press the right key
# On pressing 'Q', the program exits
# The user will not have any idea that what are the correct keys

from msvcrt import getch

def main():

    print (40 * '-')
    print ("   F R U I T - I N F O R M A T I O N")
    print (40 * '-')


    while True:
        ## Get input ###

        choice = getch()

        if choice == 'W':
                print (80 * '-')
                print ("You have chosen Orange...")
                print ("Here's the nutritional fact of the Orange:")
                print ("'One medium orange contains 1.23 grams of protein, 62 calories and 3.1 grams of dietary fiber.'")
                print (80 * '-')

        elif choice == 'A':
                print (80 * '-')
                print ("You have chosen Banana...")
                print ("Here's the nutritional fact of the Banana:")
                print ( "'One medium banana contains 1.29 grams of protein, 105 calories and 3.1 grams of dietary fiber")
                print (80 * '-')

        elif choice == 'S':
                print (80 * '-')
                print ("You have chosen Apple...")
                print ("Here's the nutritional fact of the Apple:")
                print ("'One medium apple with skin contains 0.47 grams of protein, 95 calories, and 4.4 grams of dietary fiber.'")
                print (80 * '-')


        elif choice == 'Q':
                print "See you CHUMP!"
                break;

        else:
                print "Hey mofo, choose the fruit which we have!"
                continue;
main()

When user will press 'W', the information of 'Orange will get displayed, and if user presses wrong key, it will ask to press the right one, and on pressing 'Q', the program will terminate.

Here, there is no need to wait for user to give input, as I am not asking input from user, but using getch, which will directly send the information without hitting ENTER.

I want the same functionality to be achieved to display the data in a graphical windows, using python.

When user presses 'W', then relevant information must get displayed.

I searched for Tkinter and pyHook, but not able to get any idea, as this is simple compared to the example which I have found.

What I am looking for here is an approach, how can I simplify this, rest, I will be able to to generate the code. : For me, two things which are important here is that

 -> Keyboard press event must send a listener, and then, based on validations, appropriate messages must get diplayed.

 -> Until and unless, user does not press `'Q'`, the program should continue.

Thanks.

Was it helpful?

Solution

If you want to use wxPython, then you'll probably want to catch EVT_KEY_DOWN or EVT_CHAR. Another possible solution would be to look at an AcceleratorTable. Here are some links on the subject:

Here's a typical example of binding:

self.panel.Bind(wx.EVT_KEY_DOWN, self.onKey)

Then in your event handler, onKey, you would check what button was pressed and act accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top