加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 捕获数据的简单UI

发布时间:2020-12-20 13:22:35 所属栏目:Python 来源:网络整理
导读:我知道这是一个模糊的问题,但我希望得到一些帮助.我非常了解VBA,并且能够在 python中完成一些简单的任务以及在R中完成统计编程语言. 我要做的是创建一个简单的应用程序,让我捕获数据,其中一些是从键盘捕获的.每次有击键时,我都想在我的数据集中创建一条新记
我知道这是一个模糊的问题,但我希望得到一些帮助.我非常了解VBA,并且能够在 python中完成一些简单的任务以及在R中完成统计编程语言.

我要做的是创建一个简单的应用程序,让我捕获数据,其中一些是从键盘捕获的.每次有击键时,我都想在我的数据集中创建一条新记录.

在某些情况下,考虑创建一个简单的界面,让我跟踪NHL曲棍球比赛中冰球的位置(和持续时间).

我不是一个真正的程序员,但知道足以遇到麻烦,我不确定从哪里开始.我只是在寻找一些关于非常基本(非商业)解决方案的想法.

提前谢谢了.

编辑:我想捕捉每个区域的冰球有多长.我打算使用方向键左/右“跟随”从区域到每个的冰球.每次冰球变为区域时,我想“关闭”活动记录并开始新记录.开始和结束时间将让我计算冰球在区域中的时间.我还需要一种方法来停止创建一个新的记录,比如对决,电视超时和结束时间.我打算使用空格键.我的想法是,如果我这样做是正确的,当我跟随时,记录的时间应与在电视上找到的游戏时钟上发布的内容相匹配.是的,这是一个疯狂的想法.

解决方法

如果您选择使用 Python编程:

您可以使用pygame软件包轻松捕获键盘事件.该库是为编写游戏而构建的,但可能会为您提供keydown / keyup事件所需的功能.它还处理鼠标事件(因为它适用于游戏)具有图形/文本的功能.文档非常好,它是跨平台的.一个可能的缺点是你必须有一个“屏幕”,它必须有焦点.这是一个小例子:

import pygame

def main():
    """
    Pygame Example
    """
    pygame.init()
    screen = pygame.display.set_mode((200,200)) 
    app_running = True
    while app_running:
        # Get all key/mouse events from system.
        events = pygame.event.get()
        # Loop thru each event...
        for e in events:
            # Handle when the program is killed.
            if e.type == pygame.QUIT:
                app_running = False
                break
            # Handle key events.
            elif e.type == pygame.KEYDOWN:
                # Exit if escape is pressed.
                if e.key == pygame.K_ESCAPE:
                    app_running = False
                # Do something when the right arrow
                # is pressed.
                elif e.key == pygame.K_RIGHT:
                    print "right arrow pressed"
                # Do something when the left arrow
                # is pressed.
                elif e.key == pygame.K_LEFT:
                    print "left arrow pressed"
                # and so on ...
        # Fill the screen to blank it.
        #screen.fill(mycolor)
        # Write someting to the screen to display.
        #screen.blit(some_image,some_position)
        # Flip to display.
        #screen.flip()
    pygame.quit()

if __name__ == '__main__': 
    main()

如果您使用的是Windows版本,则可以使用msvcrt库,但事件处理不如pygame:不是事件,您必须处理原始键盘输出,而且它不太直观.以下是Robert Gillies on ActiveState的小代码片段:

import msvcrt

def funkeypress():
    """
    Waits for the user to press any key including function keys. Returns 
    the ascii code for the key or the scancode for the function key.
    """
    while 1:
        if msvcrt.kbhit():                  # Key pressed?
            a = ord(msvcrt.getch())         # get first byte of keyscan code  
            if a == 0 or a == 224:          # is it a function key?
                b = ord(msvcrt.getch())     # get next byte of key scan code
                x = a + (b*256)             # cook it.
                return x                    # return cooked scancode
            else:
                return a                    # else return ascii code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读