请提供简单的 pygtk 和线程示例

编程入门 行业动态 更新时间:2024-10-23 11:19:41
本文介绍了请提供简单的 pygtk 和线程示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能给我一个简单的例子,以这种方式涉及线程.

Can someone give me a simple example involving threads in this manner, please.

我的代码的问题是,当我单击按钮 One 时,GUI 冻结直到完成.我希望按钮在执行 def 时保持响应.我该如何解决?

Problem with my code is that when I click button One, GUI freezes until its finished. I want buttons to stay responsive when def is being executed. How can i fix that?

class fun: wTree = None def __init__( self ): self.wTree = gtk.glade.XML( "ui.glade" ) dic = { "on_buttonOne" : self.one, "on_buttonTwo" : self.two, } self.wTree.signal_autoconnect( dic ) gtk.main() def sone(self, widget): time.sleep(1) print "1" time.sleep(1) print "2" time.sleep(1) print "3" def stwo(self, widget): time.sleep(1) print "4" time.sleep(1) print "5" time.sleep(1) print "6" do=fun()

请帮助我.

推荐答案

当使用 gtk 时,它将运行一个主循环,并且您将所有事情作为事件安排到 gtk 循环中.你不需要线程来做任何事情.

When using gtk, it will run a main loop, and you schedule everything you want to do as events to the gtk loop. You don't need threads to do anything.

这是一个完整的、完整的、随时可以运行的示例,它使用 glib.timeout_add 来执行您想要的操作.

Here's a complete, full, ready-to-run example that uses glib.timeout_add to do what you want.

请注意,单击两个按钮(或多次单击一个按钮)不会冻结 gui 并且所有事情都同时"发生...

Note that clicking on both buttons (or multiple times on a button) doesn't freeze the gui and everything happens "at the same time"...

import gtk import glib def yieldsleep(func): def start(*args, **kwds): iterable = func(*args, **kwds) def step(*args, **kwds): try: time = next(iterable) glib.timeout_add_seconds(time, step) except StopIteration: pass glib.idle_add(step) return start class Fun(object): def __init__(self): window = gtk.Window() vbox = gtk.VBox() btnone = gtk.Button('one') btnone.connect('clicked', self.click_one) btnone.show() vbox.pack_start(btnone) btntwo = gtk.Button('two') btntwo.connect('clicked', self.click_two) btntwo.show() vbox.pack_start(btntwo) vbox.show() window.add(vbox) window.show() @yieldsleep def click_one(self, widget, data=None): yield 1 #time.sleep(1) print '1' yield 1 #time.sleep(1) print '2' yield 1 #time.sleep(1) print '3' @yieldsleep def click_two(self, widget, data=None): yield 1 #time.sleep(1) print '4' yield 1 #time.sleep(1) print '5' yield 1 #time.sleep(1) print '6' do = Fun() gtk.main()

更多推荐

请提供简单的 pygtk 和线程示例

本文发布于:2023-11-11 07:00:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   线程   简单   pygtk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!