Request information from the user within the main GTK cycle

I am learning Python by creating a simple PyGTK application that retrieves data from some SVN repositories using pysvn . The pysvn client has a callback, you can specify what it calls when Subversion needs authentication information for the repository. When this happens, I would like to open a dialog box to ask the user for credentials.

The problem is that the callback is called inside the main GTK loop, so it basically calls every tick. Is there any way to prevent this? Perhaps opening a dialog in a new thread? But how do I return a tuple with user data in a callback so that it can return it to pysvn.Client?

+3
source share
1 answer

So we do it in RabbitVCS. Essentially, the main application creates a dialog and launches it using the PyGTK gtk.Dialog run () method .

Extract this from the main application (see action.py ):

def get_login(self, realm, username, may_save):

    # ...other code omitted...

    gtk.gdk.threads_enter()
    dialog = rabbitvcs.ui.dialog.Authentication(
        realm,
        may_save
    )
    result = dialog.run()
    gtk.gdk.threads_leave()

    return result

This get_login function is specified as a callback for the PySVN client instance.

Pay attention to the threads_enter () and threads_leave () methods! They allow GTK to use Python threads, but note that GIL may be blocked by other extensions.

, ( Glade), run() PyGTK (. dialog.py):

def run(self):
    returner = None
    self.dialog = self.get_widget("Authentication")
    result = self.dialog.run()

    login = self.get_widget("auth_login").get_text()
    password = self.get_widget("auth_password").get_text()
    save = self.get_widget("auth_save").get_active()
    self.dialog.destroy()

    if result == gtk.RESPONSE_OK:
        return (True, login, password, save)
    else:
        return (False, "", "", False)

RabbitVCS, , , , . get_widget Glade. Glade, .

, :)

+1

Source: https://habr.com/ru/post/1746177/


All Articles