How to change primary text in gtk messagedialog in python?
Question: I'm creating program in python with gtk as gui using glade. In that program, I
have several message dialog. It's simple if I just make many message dialog
for every case I have. But, is it possible to just make one message dialog and
use it for different case with different text? It's simple actually. I just
need to change primary text and show it. But, I don't find a way to change
primary text [here](http://python-
gtk-3-tutorial.readthedocs.org/en/latest/dialogs.html) and
[here](http://www.pygtk.org/docs/pygtk/class-gtkmessagedialog.html).

Below is a sample code:

    
    
    from gi.repository import Gtk
    
    def clicked1(widget):
        response = dialog1.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog1.destroy()
    
    def clicked2(widget):
        response = dialog2.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog2.destroy()
    
    def clicked3(widget):
        response = dialog3.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog3.destroy()
    
    builder = Gtk.Builder()
    builder.add_from_file('gui.glade')
    dialog1 = builder.get_object('dialog1')
    dialog2 = builder.get_object('dialog2')
    dialog3 = builder.get_object('dialog3')
    builder.get_object('button1').connect('clicked', clicked1)
    builder.get_object('button2').connect('clicked', clicked2)
    builder.get_object('button3').connect('clicked', clicked3)
    builder.get_object('window1').show_all()
    Gtk.main()
    

I want to change it to be something like this

    
    
    from gi.repository import Gtk
    
    def clicked1(widget):
        **dialog.set_text(1)**
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog.destroy()
    
    def clicked2(widget):
        **dialog.set_text(2)**
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog.destroy()
    
    def clicked3(widget):
        **dialog.set_text(3)**
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print 'ok'
        else:
            print 'cancel'
            dialog.destroy()
    
    builder = Gtk.Builder()
    builder.add_from_file('gui.glade')
    **dialog = builder.get_object('dialog')**
    builder.get_object('button1').connect('clicked', clicked1)
    builder.get_object('button2').connect('clicked', clicked2)
    builder.get_object('button3').connect('clicked', clicked3)
    builder.get_object('window1').show_all()
    Gtk.main()
    


Answer: Alternatively, we can just write some wrapping functions of
gtk_message_dialog, and here is a simple example:

    
    
    def info(text, text2=None):
        dialog = Gtk.MessageDialog(None,
                Gtk.DialogFlags.MODAL,
                Gtk.MessageType.INFO,
                Gtk.ButtonsType.OK,
                text)
        if text2 != None:
            dialog.format_secondary_text(text2)
        response = dialog.run()
        dialog.destroy()
    

The problems is that if no parent window is spesified in the message dialog,
it will show in the center of screen, rather than center of program. To solve
this problem, we can put info() function in the class and set its parent
window parameter.

Other types of message like warning, question and error can do the same way.

And a small demo:

    
    
    #!/usr/bin/env python3
    
    from gi.repository import Gtk
    
    def info(text, text2=None):
        dialog = Gtk.MessageDialog(None,
                Gtk.DialogFlags.MODAL,
                Gtk.MessageType.INFO,
                Gtk.ButtonsType.OK,
                text)
        if text2 != None:
            dialog.format_secondary_text(text2)
        response = dialog.run()
        dialog.destroy()
    
    def error(text, text2=None):
        '''
        TODO
        '''
        pass
    
    def main():
        win = Gtk.Window()
        win.connect('delete-event', Gtk.main_quit)
        win.show_all()
        info('hello')
        info('hello, primary text', 'hello secondary text')
    
        Gtk.main()
    
    if __name__ == '__main__':
        main()
    

Widget structure of a message dialog is like this: ![enter image description
here](http://i.stack.imgur.com/kjb9z.jpg)

