| 1 |
import gi |
| 2 |
|
| 3 |
gi.require_version("Gtk", "4.0") |
| 4 |
from gi.repository import Gtk |
| 5 |
|
| 6 |
|
| 7 |
class PopoverWindow(Gtk.ApplicationWindow): |
| 8 |
def __init__(self, **kargs): |
| 9 |
super().__init__(**kargs, title="Popover Demo") |
| 10 |
|
| 11 |
self.set_default_size(300, 200) |
| 12 |
|
| 13 |
box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL) |
| 14 |
box.props.halign = box.props.valign = Gtk.Align.CENTER |
| 15 |
self.set_child(box) |
| 16 |
|
| 17 |
popover = Gtk.Popover() |
| 18 |
popover_box = Gtk.Box() |
| 19 |
popover_box.append(Gtk.Label(label="Item")) |
| 20 |
popover.set_child(popover_box) |
| 21 |
|
| 22 |
button = Gtk.MenuButton(label="Click Me", popover=popover) |
| 23 |
box.append(button) |
| 24 |
|
| 25 |
button2 = Gtk.Button(label="Click Me 2") |
| 26 |
button2.connect("clicked", self.on_button_clicked) |
| 27 |
box.append(button2) |
| 28 |
|
| 29 |
self.popover2 = Gtk.Popover() |
| 30 |
self.popover2.set_child(Gtk.Label(label="Another Popup!")) |
| 31 |
self.popover2.set_parent(button2) |
| 32 |
self.popover2.props.position = Gtk.PositionType.LEFT |
| 33 |
|
| 34 |
def on_button_clicked(self, _button): |
| 35 |
self.popover2.popup() |
| 36 |
|
| 37 |
|
| 38 |
def on_activate(app): |
| 39 |
win = PopoverWindow(application=app) |
| 40 |
win.present() |
| 41 |
|
| 42 |
|
| 43 |
app = Gtk.Application(application_id="com.example.App") |
| 44 |
app.connect("activate", on_activate) |
| 45 |
|
| 46 |
app.run(None) |