简体   繁体   English

键盘中断与python gtk吗?

[英]keyboard interrupt with with python gtk?

So just like the question says, I'm trying to let keyboard interrupts happens while Gtk.main() is in progress, however, it just doesn't seem to notice that the keyboard interrupt happens until after the function is done. 因此,正如问题所言,我试图让键盘中断在Gtk.main()进行时发生,但是,似乎直到功能完成后才注意到键盘中断发生了。

So I tried sticking Gtk.main() in a separate thread, and have the main thread find the keyboard interupts, and terminate the thread, but then found out that Gtk doesn't play nicely with threads as described in this article 因此,我尝试将Gtk.main()粘贴在单独的线程中,并让主线程找到键盘中断,并终止线程,但随后发现Gtk不能很好地与本文所述的线程配合使用

I can't think of any other way to let keyboard interrupts work. 我想不出任何其他方法让键盘中断起作用。 Is this possible? 这可能吗?

I'm using python3, and want my program to eventually be cross platform. 我正在使用python3,希望我的程序最终可以跨平台。

because of https://bugzilla.gnome.org/show_bug.cgi?id=622084 gtk applications written using pygobject will not close themselves when using Ctrl + C on the terminal. 由于使用pygobject编写的https://bugzilla.gnome.org/show_bug.cgi?id=622084 gtk应用程序在终端上使用Ctrl + C时不会自行关闭。

to work around it, you can install a Unix signal handler like this: 要解决此问题,可以安装一个Unix信号处理程序,如下所示:

if __name__ == '__main__':
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    your_application_main()

The accepted answer would not work for me. 接受的答案对我不起作用。 I resolved it by replacing the Gtk.main() call with GLib.MainLoop().run() , as explained in the bug report . 错误报告中所述,我通过用GLib.MainLoop().run()替换Gtk.main()调用解决了该问题

I also ran into trouble when using the signal module to override the SIGINT handler (100% CPU on the python thread); 当使用信号模块覆盖SIGINT处理程序(python线程上100%CPU)时,我也遇到麻烦。 an alternative for me was the following: 对我来说,替代方法如下:

def main():
    self.mainloop = GObject.MainLoop()
    try:
        self.mainloop.run()
    except KeyboardInterrupt:
        logger.info('Ctrl+C hit, quitting')
        self.exit()

def exit():
    self.mainloop.quit()

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

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