試圖消除 python3 Gtk3 錯誤信息 (Trying to eliminate python3 Gtk3 error message)


問題描述

試圖消除 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!

(by rebelxtrebelxtjcoppens)

參考文件

  1. Trying to eliminate python3 Gtk3 error message (CC BY‑SA 2.5/3.0/4.0)

#linux-mint #gtk3 #python-3.x






相關問題

C/C++語言中的Conky (Conky in C/C++ language)

關閉終端不記得我對 nvm/npm 的 .profile 更改 (Closing the terminal doesnt remember my .profile changes for nvm/npm)

在 Linux Mint 中升級 Python 版本 (Upgrade Python version in Linux Mint)

試圖消除 python3 Gtk3 錯誤信息 (Trying to eliminate python3 Gtk3 error message)

在我的 Linux Mint 上安裝 NetBeans 後出現問題,它無法執行任何操作 (i have problem after installing NetBeans on my Linux Mint it can't do anything)

Linux Mint 19.1,白色背景上的白色文本 (Linux Mint 19.1, white text on white background)

PhpStorm 找不到 Linux Mint 系統變量 (PhpStorm doesn't find Linux Mint system variables)

在 Linux Mint 上安裝 Android Studio 的問題 (Problems with Installing Android Studio on Linux Mint)

如何解決 Linux 中的“npm ERR!code ELIFECYCLE”錯誤? (How to solve "npm ERR! code ELIFECYCLE" error in Linux?)

在 Linux Mint 20.1 / Ubuntu 20.04 上安裝/卸載 Docker 後網絡連接斷開 (Broken network connection after Docker install/uninstall on Linux Mint 20.1 / Ubuntu 20.04)

如何使用 Python 和 Selenium Webdriver 在 Chrome 中加載默認配置文件? (How to load default profile in Chrome using Python and the Selenium Webdriver?)

如何使用終端重命名 linux 中的目錄? (How can I rename a directory in linux with terminal?)







留言討論