单击PyGtk中的按钮时,GUI冻结 [英] GUI Freezes when clicking a button in PyGtk

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

问题描述

我知道这可能是一个反复出现的问题,但是我对该问题的其他答案没有重点.
首先,这是我的代码(如果您想突出显示语法): http://pastebin.com/9uJah8t2

I know this may be a recurring question, but I don't get the point in the other answers to that problem.
First of all, here is my code (if you want syntax highlighting) : http://pastebin.com/9uJah8t2

#!/usr/bin/python2.7
from mega import Mega
import pygtk
import gtk
import glib

class HelloWorld:
#def onSuccess(self, widget, data):


def test(self, widget, data):
    email = self.login.get_text()
    password = self.password.get_text()
    mega = Mega()
    m =  mega.login(email, password)
    details = m.get_user()
    print(details)
    #get account files
    files = m.get_files()
    print(files)

def hello(self, widget, data=None):
    print "Hello World"

def delete_event(self, widget, event, data=None):
    return False

def destroy(self, widget, data=None):
    gtk.main_quit()

def __init__(self):
    # Window Settings
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_title("Mega Connector") 
    self.window.connect("delete_event", self.delete_event)
    self.window.connect("destroy", self.destroy)
    self.window.set_border_width(5)
    self.window.set_resizable(False)

    # Vbox
    self.vbox = gtk.VBox(True, 0)

    # Login
    self.hboxlogin = gtk.HBox(True, 0)
    self.labellogin = gtk.Label("Login")
    self.hboxlogin.pack_start(self.labellogin, True, True, 0)
    self.labellogin.show()
    self.login = gtk.Entry(0)
    self.hboxlogin.pack_start(self.login, True, True, 0)
    self.login.show()
    self.hboxlogin.show()
    self.vbox.pack_start(self.hboxlogin, True, True, 0)

    # Password
    self.hboxpassword = gtk.HBox(True, 0)
    self.labelpassword = gtk.Label("Password")
    self.hboxpassword.pack_start(self.labelpassword, True, True, 0)
    self.labelpassword.show()
    self.password = gtk.Entry(0)
    self.password.set_visibility(False)
    self.hboxpassword.pack_start(self.password, True, True, 0)
    self.password.show()
    self.hboxpassword.show()
    self.vbox.pack_start(self.hboxpassword, True, True, 0)

    # Button
    self.button = gtk.Button("Connect")
    self.button.connect("clicked", self.test, None)
    self.vbox.pack_start(self.button, True, True, 0)
    self.button.show()

    self.window.add(self.vbox)
    self.vbox.show()
    self.window.show()

def main(self):
    gtk.main()

if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

这是一个非常简单的GUI,用于登录MEGA并检索有关您帐户的信息(例如上载的文件和资料).事实是,当我单击登录"时,GUI冻结,直到检索到所有信息为止.您能告诉我我在该程序中做错了什么吗?

It's a very simple GUI for loging in MEGA and retrieve informations about your account (such as uploaded files and stuff). The fact is that when I click "Login" the GUI freezes until all the informations has been retrieved. Could you please tell me what I am doing wrong in this program ?

预先感谢您的回答.

推荐答案

发生这种情况的原因是,仅当让控件返回主循环时,UI才会更新.调用 test 回调时,主循环将运行该回调,并且只有在完成回调后,控制权才会返回到主循环,并且UI可以继续更新.您只应在回调中做一些短时间的事情.有几种方法可以使长时间运行的功能起作用:

The reason this is happening is because the UI only gets updated when you let control return to the main loop. When your test callback is called, the main loop runs the callback and only when it completes does control return to the main loop and the UI can continue to update. You should only do short running things in callbacks. There are a few ways to make long running functions work:

如果Mega具有该功能的异步版本,则应使用这些功能并在回调中更新详细信息.否则,您将需要在线程中执行Mega功能.如果确实使用线程,则应注意仅在主线程上更新UI.与许多UI工具包一样,GTK + UI函数只能从主线程调用,否则它们会中断.

If Mega has an async versions of the functions you should use those and update details in a callback. Otherwise you'll need to do the Mega functions in a thread. If you do use a thread, you should be careful that you only update the UI on the main thread. Like many UI toolkits, GTK+ UI functions can only be called from the main thread or they will break.

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

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