如何使用python的curses模块创建按键事件?
我正在尝试用 python 创建一个关键事件。我认为使用curses模块我可以做到这一点,但我不知道该怎么做。请帮我。
如何通过按下键盘按键来调用函数。例如,如果按下“空格”键执行某些操作,如果按下“c”键则显示图像,如果按下“s”键则保存图像。我的问题只是完成那个关键事件。 我正在使用 Linux 操作系统
我尝试使用 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")
出现此错误:
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)
我做错了什么?我怎样才能让它发挥作用?
ps:我很抱歉我的无知,但我对 python 很陌生。
谢谢你的回答
I'm trying to make a key event in python. I think that with curses module i can do that but i don't know how to. 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. My problem is only to make that key event.
I'm using Linux o.s.
I tried to use urwid module
and when i use this code:
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.
Thank you for answer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生成按键:
在 Windows 上生成按键非常容易。在 Linux 上有点困难:
os.system('xvkbd -text "\\\\CP"')
按下按键:
对于这种情况,使用诅咒有点太多了。
Generating a keypress:
On Windows it is quite easy to generate keypresses. On Linux a bit more difficult:
os.system('xvkbd -text "\\\\CP" ')
Getting a key press:
Use of curses is a bit much for this case.
您所描述的事件通常与某种 GUI 容器(窗口、画布、框架等)相关联,因此如果没有某种 GUI,“事件”实际上没有任何意义。如果你想说你正在使用什么 GUI 框架,我可以给出更详细的答案,但除此之外,这里有一些链接描述如何使用 TKInter 和 WxPython
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. 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