2014-03-04 26 views
0

我正在编写一个程序,我的目标是显示信息,每当用户从笔记本电脑按下一个键。在Python图形窗口中显示信息

这里的程序:

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

当用户按下'W',橙色的”的信息将得到显示,如果用户按错了键,它会要求按正确的,并且在按下'Q'该程序将终止。

在这里,不需要等待用户输入,因为我不是要求用户输入,而是使用getch,它会直接发送信息而不会打到ENTER

我想要实现相同的功能,以使用python在图形窗口中显示数据。

当用户按下'W'时,必须显示相关信息。

我搜索了Tkinter and pyHook,但无法得到任何想法,因为与我找到的例子相比,这很简单。

我在这里寻找的是一种方法,我该如何简化这个,其余的,我将能够生成代码。 : 对于我来说,有两件事它们是这里重要的是

-> 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. 

感谢。

+2

你是要求某人为你使用Tkinter或pyHook编写代码吗?这太宽泛了 - 你能把它缩小到你想要的帮助。 – doctorlove

+0

我会做,我是初学者在GUI python。感谢您的建议。 – vamosrafa

回答

1

如果您想使用wxPython,那么您可能会想要捕获EVT_KEY_DOWN或EVT_CHAR。另一个可能的解决方案是查看AcceleratorTable。以下是关于这个问题的一些链接:

这里是结合一个典型的例子:

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

然后在你的事件处理程序,onKey,你会检查按下了什么按钮,并相应地采取行动。

相关问题