简体   繁体   English

使用 PyGObject、Libadwaita 在 GTK 4 中创建 Flap

[英]Create Flap in GTK 4 with PyGObject, Libadwaita

I'm trying to create a Flap in Libadwaita using PyGObject but I only get an empty window.我正在尝试使用 PyGObject 在 Libadwaita 中创建一个 Flap,但我只得到一个空的 window。

My code looks like this:我的代码如下所示:

import gi

gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')

from gi.repository import Gio, Gtk
from gi.repository import Adw

class MainWindow(Gtk.ApplicationWindow):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.set_title(title='Python and GTK 4 Adwaita: PyGObject Adw.Flap')

        box = Gtk.Box()

        self.set_child(child=box)

        adw_flap = Adw.Flap.new()

        self.set_child(child=adw_flap)


class Application(Gtk.Application):

    def __init__(self):
        super().__init__(application_id='adw.flap.demo',
                         flags=Gio.ApplicationFlags.FLAGS_NONE)

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = MainWindow(application=self)
        win.present()

    def do_shutdown(self):
        Gtk.Application.do_shutdown(self)


if __name__ == '__main__':
    import sys

    app = Application()
    app.run(sys.argv)

I'm creating a GTK window with a box inside (do I need to create the box or can I place the flap directly inside the GTK window?) and inside the box I'm trying to create the flap. I'm creating a GTK window with a box inside (do I need to create the box or can I place the flap directly inside the GTK window?) and inside the box I'm trying to create the flap. Where am I going wrong in my code?我的代码哪里出错了?

I made an example using Adw.Flap() together with Gtk.StackSidebar().我使用 Adw.Flap() 和 Gtk.StackSidebar() 做了一个例子。

# -*- coding: utf-8 -*-
"""Python e GTK 4: PyGObject Adw.Flap()."""

import gi

gi.require_version(namespace='Gtk', version='4.0')
gi.require_version(namespace='Adw', version='1')

from gi.repository import Gio, Gtk
from gi.repository import Adw


class MainWindow(Gtk.ApplicationWindow):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.set_title(title='Python e GTK 4: PyGObject Adw.Flap()')
        self.set_default_size(width=int(1366 / 2), height=int(768 / 2))
        self.set_size_request(width=int(1366 / 2), height=int(768 / 2))

        vbox = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        self.set_child(child=vbox)

        headerbar = Gtk.HeaderBar.new()
        self.set_titlebar(titlebar=headerbar)

        # Botão que abre e fecha o Flap.
        flap_Toggle_button = Gtk.ToggleButton.new()
        flap_Toggle_button.set_icon_name(icon_name='sidebar-show-right-rtl-symbolic')
        flap_Toggle_button.connect('clicked', self.on_flap_button_toggled)
        headerbar.pack_start(child=flap_Toggle_button)

        self.adw_flap = Adw.Flap.new()
        self.adw_flap.set_reveal_flap(reveal_flap=False)
        self.adw_flap.set_locked(locked=True)

        vbox.append(child=self.adw_flap)

        stack = Gtk.Stack.new()
        self.adw_flap.set_content(content=stack)

        # Página 1
        box_page_1 = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        stack.add_titled(child=box_page_1, name='pagina1', title='Página 1')

        label_page_1 = Gtk.Label.new(str='Página 1')
        label_page_1.set_halign(align=Gtk.Align.CENTER)
        label_page_1.set_valign(align=Gtk.Align.CENTER)
        label_page_1.set_hexpand(expand=True)
        label_page_1.set_vexpand(expand=True)
        box_page_1.append(child=label_page_1)

        # Página 2
        box_page_2 = Gtk.Box.new(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        stack.add_titled(child=box_page_2, name='pagina2', title='Página 2')

        label_page_2 = Gtk.Label.new(str='Página 2')
        label_page_2.set_halign(align=Gtk.Align.CENTER)
        label_page_2.set_valign(align=Gtk.Align.CENTER)
        label_page_2.set_hexpand(expand=True)
        label_page_2.set_vexpand(expand=True)
        box_page_2.append(child=label_page_2)

        # StackSidebar gerencia a troca entre os stack.
        stack_sidebar = Gtk.StackSidebar.new()
        stack_sidebar.set_stack(stack=stack)
        self.adw_flap.set_flap(flap=stack_sidebar)

    def on_flap_button_toggled(self, widget):
        self.adw_flap.set_reveal_flap(not self.adw_flap.get_reveal_flap())


class Application(Adw.Application):

    def __init__(self):
        super().__init__(application_id='br.natorsc.Exemplo',
                         flags=Gio.ApplicationFlags.FLAGS_NONE)

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = MainWindow(application=self)
        win.present()

    def do_shutdown(self):
        Gtk.Application.do_shutdown(self)


if __name__ == '__main__':
    import sys

    app = Application()
    app.run(sys.argv)

在此处输入图像描述

The code used as an example is in my repository:用作示例的代码在我的存储库中:

Whenever possible I am adding new examples.只要有可能,我就会添加新的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM