5

Good morning,

I'm trying to add a Gtk.Entry to a Gtk.MessageDialog. With the following code it seems that I added the Gtk.Entry but it's not visible on the dialog window (Python3/Gtk3):

#!/usr/bin/python3

from gi.repository import Gtk

def get_user_pw(parent, message, default=''):
    dialogWindow = Gtk.MessageDialog(parent,
                          Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                          Gtk.MessageType.QUESTION,
                          Gtk.ButtonsType.OK_CANCEL,
                          message)

    dialogBox = dialogWindow.get_content_area()
    userEntry = Gtk.Entry()
    userEntry.set_visibility(False)
    userEntry.set_invisible_char("*")
    userEntry.set_size_request(250,0)
    userEntry.set_text("Test")
    dialogBox.pack_end(userEntry, False, False, 0)
    #dialogWindow.vbox.pack_start(userEntry, False, False, 0)

    response = dialogWindow.run()
    text = userEntry.get_text() 
    dialogWindow.destroy()
    if response == Gtk.ResponseType.OK:
        return text
    else:
        return None

class MainWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="MyWindowTitle")

        userPassphrase = get_user_pw(self, "SSH key passphrase")
        print("User passphrase: " + userPassphrase)

This code prints :

User passphrase: Test

I'm looking for clues about making the entry visible and editable, any help is welcome.

References:
http://python-gtk-3-tutorial.readthedocs.org/en/latest/dialogs.html
http://developer.gnome.org/gtk3/3.2/GtkDialog.html
Simple, versatile and re-usable entry dialog (sometimes referred to as input dialog) in PyGTK


2
  • you can answer your own question and accept the answer, so that the question doesn't remain open. Commented Feb 12, 2014 at 19:15
  • I couldn't do that at the time. I didn't have enough reputation points I think. Now it's done. Commented Feb 13, 2014 at 14:04

2 Answers 2

11

Ok it works now, I needed to show_all() before run(). It took me some times to figure out this simple thing. Debugged code is :

def get_user_pw(parent, message, title=''):
    # Returns user input as a string or None
    # If user does not input text it returns None, NOT AN EMPTY STRING.
    dialogWindow = Gtk.MessageDialog(parent,
                          Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                          Gtk.MessageType.QUESTION,
                          Gtk.ButtonsType.OK_CANCEL,
                          message)

    dialogWindow.set_title(title)

    dialogBox = dialogWindow.get_content_area()
    userEntry = Gtk.Entry()
    userEntry.set_visibility(False)
    userEntry.set_invisible_char("*")
    userEntry.set_size_request(250,0)
    dialogBox.pack_end(userEntry, False, False, 0)

    dialogWindow.show_all()
    response = dialogWindow.run()
    text = userEntry.get_text() 
    dialogWindow.destroy()
    if (response == Gtk.ResponseType.OK) and (text != ''):
        return text
    else:
        return None

I use it like this :

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="MyWindowTitle")
        userPassword = get_user_pw(self, "Please enter your password", "Password")
Sign up to request clarification or add additional context in comments.

Comments

1

This may be going about it the hard way if this is just to run a sudo command - you could simply call

os.system('pkexec (yourcommand)')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.