基于类,我有一个包含button
和progressbar
的窗口,每当点击按钮时,就会发生两件事情:
1 - 应该从对话框传递给
class ABCD
2 - 当我们的班级
ABCD()
完成他的工作时,我们的进度条应该进行常规脉冲,直到ABCD()类完成过程。
所以问题是进度条脉冲只有一次,然后一直停留在ABCD()类完成,然后它开始定时脉动。
这是我的尝试:
import gi,time
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
self.Myinput = Gtk.Entry()
box = self.get_content_area()
box.add(self.Myinput)
self.show_all()
class DialogWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Dialog Example")
self.set_border_width(6)
Hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.add(Hbox)
self.button = Gtk.Button("Open dialog")
self.button.connect("clicked", self.on_button_clicked)
Hbox.pack_start(self.button, True, True, 0)
self.progressbar = Gtk.ProgressBar()
Hbox.pack_start(self.progressbar, True, True, 0)
#~~~~~~ Progress Bar
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return True
def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
variable = dialog.Myinput.get_text()
print("start")
dialog.destroy()
#ProgressBar time function
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
self.progressbar.pulse()
#this for Updating the Windows and make the progressbar pulsing while waiting
# the class ABCD finish his stuff, finally should stop pulsing.
while Gtk.events_pending():
Gtk.main_iteration_do(False)
passing_instance = ABCD(variable)
class ABCD(object):
def __init__(self,value_of_dialog):
self.get_value = value_of_dialog
self.for_add = "______ add was done"
self.final_value = self.get_value+self.for_add
time.sleep(10)
print("gonna be finished")
print(self.final_value)
win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
正如我们在这里看到的,我已经尝试在这部分代码中制作脉冲并刷新窗口
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
self.progressbar.pulse()
#this for Updating the Windows and make the progressbar pulsing while waiting
# the class ABCD finish his stuff, finally should stop pulsing.
while Gtk.events_pending():
Gtk.main_iteration_do(False)
否则因为在
my class ABCD()
我应该time.sleep(10)
进度条脉冲仅在10秒后才开始 停止。
这段代码应该如何,我需要有人为我提供正确的代码,几乎没有解释。
答案 0 :(得分:1)
使用sleep
来模拟时间的流逝的问题是sleep
将停止线程中发生的所有事情,在这种情况下会阻止线程到达Gtk.main()
这是进度条脉冲或更新所必需的。
因此,为了正确地做到这一点,有两个选择:
在一个单独的线程中运行ABCD
,以便主线程可以到达Gtk.main()
。这将确保进度条按预期移动。一个简单的例子如下:
self.abcd_thread = ABCD(variable)
self.abcd_thread.start()
class ABCD(Thread):
def __init__(self, value_of_dialog):
super(ABCD, self).__init__()
self.get_value = value_of_dialog
self.for_add = "______ add was done"
self.final_value = self.get_value+self.for_add
def run(self):
print "Starting " + self.name
time.sleep(10)
print("gonna be finished")
print(self.final_value)
print "Exiting " + self.name
使用此功能时,您可以使用self.abcd_thread.isAlive()
查看该线程是否仍在计算内容。返回信息的方式很大程度上取决于线程中的工作。
将time.sleep
替换为以下片段:
now = time.time()
while time.time() - now < 10:
# Insert any code here
Gtk.main_iteration_do(False)
这仍然会模仿ABCD
做10秒的事情,但因为我们在循环的每次迭代中调用Gtk.main_iteration_do(False)
,GTK能够在循环期间更新接口。
一般来说,第二个选项最简单,因为它只涉及在你做任何事情时拨打Gtk.main_iteration_do(False)
。另一方面,第一个选项在处理复杂计算时更有用,其中添加Gtk调用并不容易。