简体   繁体   中英

Trying to eliminate python3 Gtk3 error message

When I run this script:

#!/usr/bin/env python3

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

def display_message_dialog(button, message_type, window):
    messagedialog = Gtk.MessageDialog(message_format="MessageDialog")
    messagedialog.set_property("message-type", message_type)
#    messagedialog.set_parent(window)
    messagedialog.run()
    messagedialog.destroy()

window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())

grid = Gtk.Grid()
grid.set_column_spacing(5)
window.add(grid)

buttonInfo = Gtk.Button(label="Information")
buttonInfo.connect("clicked", display_message_dialog, Gtk.MessageType.INFO, window)
grid.attach(buttonInfo, 0, 0, 1, 1)
buttonError = Gtk.Button(label="Error")
buttonError.connect("clicked", display_message_dialog, Gtk.MessageType.ERROR, window)
grid.attach(buttonError, 3, 0, 1, 1)

window.show_all()
Gtk.main()

I get this error message when clicking one of the buttons in the window:

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.

Uncommenting the messagedialog.set_parent(window) statement adds this message:

Gtk-WARNING **: Can't set a parent on a toplevel widget

What do I have to do to eliminate these messages?

I am using Linux Mint 18 Mate.

What works is adding parent=window to the MessageDialog statement:

messagedialog = Gtk.MessageDialog(message_format="MessageDialog", parent=window)

I still don't know why the set_parent statement is ineffective.

Here is an example which works with either parent = as a property (as it is shown) or you can eliminate the property, and uncomment dlg.set_transient_for(self) (I believe set_parent isn't the right function, as the relation needed here is to get an event if the parent window is closed)

from gi.repository import Gtk

VERSION = "0.1"

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())

        btn = Gtk.Button("Call about dialog")
        btn.connect("clicked", self.btn_clicked)

        self.add(btn)
        self.show_all()

    def btn_clicked(self, btn):
        dlg = Gtk.AboutDialog(version = VERSION,
                              program_name = "SomeTest",
                              parent = self,
                              license_type = Gtk.License.GPL_3_0)
        #dlg.set_transient_for(self)
        dlg.run()
        dlg.destroy()

    def run(self):
        Gtk.main()

def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

Anyway, both forms work error-less!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM