简体   繁体   English

如何使用python curses模块创建一个关键事件?

[英]How to use python curses module to create a key event?

I'm trying to make a key event in python. 我正在尝试在python中创建一个关键事件。 I think that with curses module i can do that but i don't know how to. 我认为使用curses模块我可以做到,但我不知道如何。 Please help me. 请帮我。

How can i call a function with a press of a keyboard key. 如何通过按键盘键来调用功能。 Like, if "space" key is pressed do something, if "c" key is pressed show image, if "s" key is pressed save image. 就像,如果按下“空格”键做某事,如果按“c”键显示图像,如果按“s”键保存图像。 My problem is only to make that key event. 我的问题只是制作那个关键事件。 I'm using Linux os 我正在使用Linux操作系统

I tried to use urwid module and when i use this code: 我尝试使用urwid模块,当我使用此代码时:

import PIL
import Image 
im=Image.open("im.tif")
imshow(im,cmap=cm.gray ,origin=1)
import urwid 
def save(input):
     if input in ('s'):
         savefig("im2.png")

appeared this error: 出现了这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py", line 312, in key_press
    FigureCanvasBase.key_press_event(self, key, guiEvent=event)
  File "/usr/lib/pymodules/python2.6/matplotlib/backend_bases.py", line 1143, in key_press_event
    self.callbacks.process(s, event)
  File "/usr/lib/pymodules/python2.6/matplotlib/cbook.py", line 163, in process
    func(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/matplotlib/backend_bases.py", line 1703, in key_press
    self.canvas.toolbar.save_figure(self.canvas.toolbar)
TypeError: save_figure() takes exactly 1 argument (2 given)

What am i doing wrong ? 我究竟做错了什么 ? How can i make it work? 我怎样才能使它工作?

ps: I'm sorry for my ignorance but i'm very new in python. ps:我很抱歉我的无知,但我是蟒蛇新手。

Thank you for answer 谢谢你的答案

Generating a keypress: 生成按键:

On Windows it is quite easy to generate keypresses. 在Windows上,生成按键很容易。 On Linux a bit more difficult: 在Linux上有点困难:

Getting a key press: 获得关键新闻:

Use of curses is a bit much for this case. 对于这种情况,使用curses有点多。

Events such as you describe are usually associated with some sort of GUI container (window, canvas, frame, what have you) so "events" really don't have any meaning without some sort of GUI. 你描述的事件通常与某种GUI容器(窗口,画布,框架,你有什么)相关联,所以没有某种GUI,“事件”真的没有任何意义。 I could give a more detailed answer if you would say what GUI framework you are using, but barring that, here are links descibing how to handle events using TKInter and WxPython 如果您要说明您正在使用的GUI框架,我可以给出更详细的答案,但除此之外,这里有链接描述如何使用TKInterWxPython处理事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM