問題描述
試圖消除 python3 Gtk3 錯誤信息 (Trying to eliminate python3 Gtk3 error message)
當我運行這個腳本時:
#!/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()
我在單擊窗口中的一個按鈕時收到此錯誤消息:
Gtk‑Message: GtkDialog mapped without a短暫的父母。不鼓勵這樣做。
取消註釋 messagedialog.set_parent(window) 語句會添加此消息:
Gtk‑WARNING ** : 無法在頂級小部件上設置父級
我必須做些什麼來消除這些消息?
我正在使用 Linux Mint 18 Mate。
p>參考解法
方法 1:
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.
方法 2:
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!