有一个弹出窗口保持在父级Glade,GTK,Python上

2024-04-24 16:03:28 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能告诉我,即使在移动父窗口的同时,如何使首选项弹出窗口保持在父窗口的中心?谢谢:)


Tags: 中心首选项
1条回答
网友
1楼 · 发布于 2024-04-24 16:03:28

下面是一个非常简单的例子,首选项窗口居中,在移动它的同时父窗口也将移动(但取决于窗口管理器):

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

class AppWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Python Example")

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=6)
        self.add(box)

        button1 = Gtk.Button("Information")
        button1.connect("clicked", self.on_button_clicked)
        box.add(button1)

        treeview = Gtk.TreeView()
        treeview.set_size_request(640,480)
        box.add(treeview)

    def on_button_clicked(self, widget):
        preferences_window = Gtk.Window (Gtk.WindowType.TOPLEVEL)
        preferences_window.set_title ("Preferences Window")
        preferences_window.set_modal (True)
        preferences_window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        preferences_window.set_transient_for(self)
        preferences_window.show_all()
        preferences_window.connect("delete-event", preferences_window.destroy);

win = AppWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

导致: enter image description here

相关问题 更多 >