英文:
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,如何使我的应用程序使用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:
But it looks like this:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论