GUI Freezes when clicking a button in PyGtk

本小妞迷上赌 提交于 2021-02-11 13:10:33

问题


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()

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 ?

Thanks in advance for your answers.


回答1:


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:

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.



来源:https://stackoverflow.com/questions/15063932/gui-freezes-when-clicking-a-button-in-pygtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!