Libadwaita主题在Python中不起作用。

huangapple go评论73阅读模式
英文:

Libadwaita themeing is not working in python

问题

这是我第一次在Python中使用pyGOObject和gtk。我想要使用libadwaita制作一个应用程序,并提供了以下代码:

import sys
import gi

gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')

from gi.repository import Gtk, Adw

class MainWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_title('Test')
        self.set_default_size(800, 600)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.set_child(box)

        button = Gtk.Button.new_with_label("Test")
        button.connect("clicked", self.on_button_clicked)
        box.append(button)

    def on_button_clicked(self, button):
        print("Button was clicked")

class MyApp(Adw.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app)
        self.win.present()

app = MyApp(application_id='com.example.GtkApplication')
app.run(sys.argv)

代码能够运行,但问题是它看起来不像libadwaita,而是像gtk!顶部栏应该是这样的:

Libadwaita主题在Python中不起作用。

但它看起来是这样的:

Libadwaita主题在Python中不起作用。

我认为那不是libadwaita,如何使我的应用程序使用libadwaita?小部件和元素看起来像Libadwaita,但顶部栏不是。

英文:

This is my first time using pyGOObject and gtk in Python. I want to make an app with libadwaita and came up with this:

import sys
import gi

gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')

from gi.repository import Gtk, Adw

class MainWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_title('Test')
        self.set_default_size(800, 600)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.set_child(box)

        button = Gtk.Button.new_with_label("Test")
        button.connect("clicked", self.on_button_clicked)
        box.append(button)

    def on_button_clicked(self, button):
        print("Button was clicked")

class MyApp(Adw.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app)
        self.win.present()

app = MyApp(application_id='com.example.GtkApplication')
app.run(sys.argv)

The code runs, but the problem is it doesn't look like libadwaita, it just looks like gtk!

The top bar should look like this:

Libadwaita主题在Python中不起作用。

But it looks like this:

Libadwaita主题在Python中不起作用。

I assume that's not libadwaita, how can I make my app use libadwaita?
The widgets and elements look like Libadwaita but not the top bar.

答案1

得分: 1

当使用libadwaita时,你应该继承自Adw.ApplicationWindow,像这样:

import sys
import gi

gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')

from gi.repository import Gtk, Adw

class MainWindow(Adw.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_title('Test')
        self.set_default_size(800, 600)

        # ... 这里是你的部件

class MyApp(Adw.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app)
        self.win.present()

app = MyApp(application_id='com.example.GtkApplication')
app.run(sys.argv)

请注意,我只提供了代码部分的翻译,不包括问题的其他内容。

英文:

When using libadwaita you should inherit from Adw.ApplicationWindow, like so:

import sys
import gi

gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')

from gi.repository import Gtk, Adw

class MainWindow(Adw.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_title('Test')
        self.set_default_size(800, 600)

        # ... Your widgets here

class MyApp(Adw.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        self.win = MainWindow(application=app)
        self.win.present()

app = MyApp(application_id='com.example.GtkApplication')
app.run(sys.argv)

huangapple
  • 本文由 发表于 2023年5月10日 23:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定