单击python GTK3中的按钮后,窗口冻结 [英] Window freezes after clicking of button in python GTK3

查看:78
本文介绍了单击python GTK3中的按钮后,窗口冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一些命令平均运行30分钟,当我单击GTK3创建的按钮时,python开始执行命令,但我的所有应用程序都冻结了.我点击按钮的python代码是:

Hello I have some command, which runs for average 30 min, when I click on button created by GTK3, python starts to executing command but my all application freezes. My python code for button clicked is:

def on_next2_clicked(self,button):
    cmd = "My Command"
    proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    while True:
            line = proc.stdout.read(2)
            if not line:
                break
            self.fper = float(line)/100.0
            self.ui.progressbar1.set_fraction(self.fper)
    print "Done"

我还必须在窗口中将命令输出设置为进度条.任何人都可以帮助解决我的问题吗?我也尝试过在python中使用Threading,但是它也没有用...

I also have to set output of command to progress bar in my window. Can any one help to solve my problem ? I also tried with Threading in python, but it also falls useless...

推荐答案

从循环内部运行主循环迭代:

Run a main loop iteration from within your loop:

def on_next2_clicked(self,button):
    cmd = "My Command"
    proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    while True:
        line = proc.stdout.read(2)
        if not line:
            break
        self.fper = float(line)/100.0
        self.ui.progressbar1.set_fraction(self.fper)
        while Gtk.events_pending():
            Gtk.main_iteration()  # runs the GTK main loop as needed
    print "Done"

这篇关于单击python GTK3中的按钮后,窗口冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆