蟒蛇。使用Gtk GUI在背景上做一些工作 [英] Python. Doing some work on background with Gtk GUI
问题描述
我正在尝试显示GUI并执行一些在后台工作。据我了解,它应该看起来像这样:
#!/ usr / bin / env python3
# - * - 编码:utf-8 - * -
从线程导入导入时间
线程
from gi.repository import Gtk,Gdk
class Gui():
def run(self):
self.Window = Gtk.Window()
self.Window.set_border_width(8)
self.Window.set_title (НекийGUI)
self.Window.connect('destroy',lambda x:self.stop())
self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK )
self.outBut.set_size_request(150,35)
self.outBut.connect('clicked',lambda x:self.passfun)
self.Window.add(self.outBut)
self.Window.show_all()
def stop(self):
Gtk.main_quit()
$ b $ def passfun(self ):
传递
类LoopSleep(线程):
def run(self):
i = 1
而True:
print(i)
i = i + 1
#time .sleep(1)
gui = Gui()
gui.run()
loopSleep = LoopSleep()
loopSleep.start()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
但它不起作用。按下按钮时会出现几个循环。窗口关闭后,循环运行。但不是在一起。
我做错了什么?
Can并不声称是python线程的专家,也不是gtk3,但是在你的例子中玩了一下之后,我发现了一些看起来像你想要的那样工作的东西。而不是子分类线程我使用threading.start(target = loop_sleep),并将其放在Gui内。
Glib.threads_init()似乎也是需要的。 p>
#!/ usr / bin / env python3
from gi.repository import Gtk,Gdk,GLib
import线程
导入时间
$ b $ class Gui(Gtk.Window):
def __init __(self):
self.Window = Gtk.Window()
self.Window.set_border_width(8)
self.Window.set_title(НекийGUI)
self.Window.connect('destroy',lambda x:self.stop())
self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK)
self.outBut.set_size_request(150,35)
self.Window.connect('destroy',lambda x:self .stop())
self.Window.add(self.outBut)
self.Window.show_all()
threading.Thread(target = loop_sleep).start()
def stop(self):
Gtk.main_quit()
$ b $ def passfun(self):
pass
d ef loop_sleep():
i = 1
而真:
print(i)
i = i + 1
#time.sleep(1)
$ b app = Gui()
GLib.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main ()
Gdk.threads_leave()
- python 3.2.2
- gtk3 3.2.2
- python-gobject 3.0.2
I'm trying to display a GUI and do some work in the background. As I understand it should look something like this:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
from threading import Thread
from gi.repository import Gtk, Gdk
class Gui():
def run(self):
self.Window = Gtk.Window()
self.Window.set_border_width(8)
self.Window.set_title("Некий GUI")
self.Window.connect('destroy', lambda x: self.stop())
self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK)
self.outBut.set_size_request(150, 35)
self.outBut.connect('clicked', lambda x: self.passfun)
self.Window.add(self.outBut)
self.Window.show_all()
def stop(self):
Gtk.main_quit()
def passfun(self):
pass
class LoopSleep(Thread):
def run(self):
i = 1
while True:
print(i)
i = i + 1
#time.sleep(1)
gui = Gui()
gui.run()
loopSleep = LoopSleep()
loopSleep.start()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
But it does not work. Several cycles occurs when you press the button. And the cycle runs after the window is closed. But not together.
What I do wrong?
Can't claim to be any expert on python threading nor gtk3 but after playing around a little with your example I found something that appears to work the way you want it. Instead of sub classing Thread i use threading.start(target=loop_sleep), and placed that inside Gui.
Glib.threads_init() also seem to be needed.
#!/usr/bin/env python3
from gi.repository import Gtk,Gdk, GLib
import threading
import time
class Gui(Gtk.Window):
def __init__(self):
self.Window = Gtk.Window()
self.Window.set_border_width(8)
self.Window.set_title("Некий GUI")
self.Window.connect('destroy', lambda x: self.stop())
self.outBut = Gtk.Button.new_from_stock(Gtk.STOCK_OK)
self.outBut.set_size_request(150, 35)
self.Window.connect('destroy', lambda x: self.stop())
self.Window.add(self.outBut)
self.Window.show_all()
threading.Thread(target=loop_sleep).start()
def stop(self):
Gtk.main_quit()
def passfun(self):
pass
def loop_sleep():
i = 1
while True:
print(i)
i = i + 1
#time.sleep(1)
app = Gui()
GLib.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
这篇关于蟒蛇。使用Gtk GUI在背景上做一些工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!