Gtk.Dialog的位置

Position of Gtk.Dialog

是否可以更改 Gtk.Dialog 的位置?

此刻它突然出现在我的 main-Window 的正中央。我可以设置值,使其更偏向左侧或右侧吗?

谢谢!

这是一个简单的示例,说明您可能希望如何将 Gtk.Dialog 移动到您想要的位置:

import gi

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


class DialogExample(Gtk.Dialog):

    def __init__(self, parent, x, y):
        Gtk.Dialog.__init__(self, title="My Dialog", transient_for=parent, flags=0)

        self.set_default_size(150, 50)

        # Get current position of dialog
        pos = self.get_position()
        # Move dialog to the desired location
        self.move(pos[0] + x, pos[1] + y)

        label = Gtk.Label(label="This is a dialog to display additional information")

        box = self.get_content_area()
        box.add(label)
        self.show_all()


class DialogWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Dialog Example")

        self.set_border_width(6)

        button = Gtk.Button(label="Open dialog")
        button.connect("clicked", self.on_button_clicked)

        self.add(button)

    def on_button_clicked(self, widget):
        # Open dialog 200 px to the left relative to main window
        dialog = DialogExample(self, -200, 0) 
        dialog.run()
        dialog.destroy()


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

来源: