I need to pass a message to an error dialog, to display it to the user.

First attempt

PreferencesDialog.py

dlgError = ErrorDialog ('Have you been messing with the configuration files?')
dlgError.run ()

ErrorDialog.py

global message = None

def __new__(cls, msg):
    builder = get_builder('ErrorDialog')
    new_object = builder.get_object('error_dialog')
    new_object.finish_initializing(builder)

    global message
    message = msg

    return new_object

def finish_initializing(self, builder):

    # Get a reference to the builder and set up the signals.
    self.builder = builder
    self.ui = builder.get_ui(self)

    global message
    self.builder.get_object ('lblMessage').set_text (message)

Second attempt

PreferencesDialog.py

dlgError = ErrorDialog ()
dlgError.message = 'Have you been messing with the configuration files?'
dlgError.run ()

ErrorDialog.py

global message = None

def __new__(cls):
    builder = get_builder('ErrorDialog')
    new_object = builder.get_object('error_dialog')
    new_object.finish_initializing(builder)

    return new_object

def finish_initializing(self, builder):

    # Get a reference to the builder and set up the signals.
    self.builder = builder
    self.ui = builder.get_ui(self)

    global message
    self.builder.get_object ('lblMessage').set_text (message)

Both throw the same exception;

self.builder.get_object ('lblMessage').set_text (message)
NameError: global name 'message' is not defined

Please assist. I'm using Quickly, GTK 3 and Python 2.7.

share|improve this question

I setup a dialog where I passed my dialog a variable to put the text in an entry box. Trick is to pass the variable into new and then when finish.initializing() is called put it in there. Here's the code:

class QuerynameDialog(Gtk.Dialog):
    __gtype_name__ = "QuerynameDialog"

    def __new__(cls, name):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.

        Returns a fully instantiated QuerynameDialog object.
        """
        builder = get_builder('QuerynameDialog')
        new_object = builder.get_object('queryname_dialog')
        new_object.finish_initializing(builder,name)
        return new_object


    def finish_initializing(self, builder,name):
        """Called when we're finished initializing.

        finish_initalizing should be called after parsing the ui definition
        and creating a QuerynameDialog object with it in order to
        finish initializing the start of the new QuerynameDialog
        instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self)
        self.builder.get_object('name_entry').set_text(name)
share|improve this answer

Why not make the message a member of the class instance, by setting it in __init__?

def __init__(self, msg):
  self._message = msg

def finish_initializing(self, builder):
  self.builder.get_object("lblMessage").set_text(self._message)
share|improve this answer
    
TypeError: __new__() takes exactly 1 argument (2 given) – RobinJ Jun 27 '12 at 11:53
    
@RobinJ This is one of the reasons I find the Quickly stub code annoying... But that is easy to repair: replace def __new__(cls, msg) by def __new__(cls, msg, *args, **kwargs) as explained here. After that, you can use unwind solution, which I find is the best one. – brandizzi Jul 22 '12 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.