我知道这是一个模糊的问题,但我希望得到一些帮助。我非常了解 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
关于python - 用于捕获数据的简单 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410244/