[Python] Gtk.MessageDialog inherited class throws PyGTKDeprecationWarning warnings

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I use a custom class for several projects that inherits Gtk.MessageDialog. Just out of curiosity I ran the script with -Wd:

    python3 -Wd dialogs_test.py


    Using "parent=" in __init__ I get the following warnings:

    .../dialogs_test.py:15: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "parent" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations
    super().__init__(self,

    /usr/lib/python3/dist-packages/gi/overrides/Gtk.py:577: PyGTKDeprecationWarning: The keyword(s) "parent" have been deprecated in favor of "transient_for" respectively. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations


    Replace "parent=" with "transient_for=" in __init__:

    See above
    ...
    TypeError: could not convert value for property `transient_for' from Dialog to GtkWindow


    I use keywords in __init__. So, what am I missing with the first warning?

    I cannot find how to use transient_for or if I even have to. Does anybody know where to find the documentation on that?

    This is my simplified script:

    #!/usr/bin/env python3

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk


    class Dialog(Gtk.MessageDialog):
    def __init__(self, message_type, buttons, title, text):

    # Search for parent
    parent = next((w for w in Gtk.Window.list_toplevels() if w.get_title()), None)

    # Initialize the dialog object
    super().__init__(self,
    parent=parent,
    message_type=message_type,
    buttons=buttons,
    text=text)

    # Set title
    self.set_title(title)

    def show_dialog(self):
    """ Show the dialog """
    response = self.run() in (Gtk.ResponseType.YES,
    Gtk.ResponseType.APPLY,
    Gtk.ResponseType.OK,
    Gtk.ResponseType.ACCEPT)
    self.destroy()
    return response

    def message_dialog(*args, **kwargs):
    """ Show message dialog """
    #return kwargs
    return Dialog(Gtk.MessageType.INFO, Gtk.ButtonsType.OK, *args, **kwargs).show_dialog()


    class MessageDialogWindow(Gtk.Window):

    def __init__(self):
    super().__init__(title="MessageDialog Test")
    self.set_default_size(500, 100)
    box = Gtk.Box()
    self.add(box)

    button = Gtk.Button.new_with_label("Info dialog by class")
    button.connect("clicked", self.on_info_clicked)
    button.set_hexpand(True)
    box.add(button)

    def on_info_clicked(self, widget):
    result = message_dialog(title='button title', text='button text')
    print((f"button result: {result}"))


    win = MessageDialogWindow()
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()


    [SOLUTION]

    The solution is to leave out "self" and replace "parent=" with "transient_for=" in __init__:

    super().__init__(transient_for=parent,
    message_type=message_type,
    buttons=buttons,
    text=text)

    Continue reading...

Compartilhe esta Página