Python+PYGObject/PYGtk+CSS样式--2024python示例

隔久点不用老是会忘,留个笔记。。

Python+PYGObject/PYGtk,加载 CSS 样式的演示代码

demo 运行的效果截图:

python 复制代码
#!/usr/bin/env python3
import sys
import gi

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


class MyApplication(Gtk.Application):
	def __init__(self):
		super().__init__(application_id="com.example.MyGtkApplication")
		GLib.set_application_name('My Gtk Application')

	def do_activate(self):
		window = Gtk.ApplicationWindow(application=self, title="Hello World")
		window.present()
		button = Gtk.Button(label="Python+PyGObject+CSS修改背景色")
		button.set_name("bricks-button")
		button.set_halign(Gtk.Align.CENTER)
		button.set_valign(Gtk.Align.CENTER)
		button.set_size_request(250, 84)
		window.add(button)
		window.show_all()

		strcss="""
			#bricks-button {
				color: #F55;
				background-color: #eef;
				background-image: -gtk-scaled(url('resource://css/brick.png'), url('resource://css/brick2.png'));
				background-repeat: no-repeat;
				background-position: center;
				/* 测试 */
			}
		"""
		
		strpath="./play.css"
		fp = open(strpath, 'w')
		fp.write(strcss)
		fp.close()

		provider = Gtk.CssProvider()
		provider.connect("parsing-error", self.show_css_parsing_error)
		# fname = Gio.file_new_for_path('play.css')
		# provider.load_from_file(fname)
		# https://docs.gtk.org/gtk4/method.CssProvider.load_from_file.html
		# load_from_path, load_from_file, load_from_string
		provider.load_from_path(strpath)
		self.apply_css(window, provider)

	def show_css_parsing_error(self, provider, section, error):
		print(error.message)

	def apply_css(self, widget, provider):
		Gtk.StyleContext.add_provider(widget.get_style_context(), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

		if isinstance(widget, Gtk.Container):
			widget.forall(self.apply_css, provider)


app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

官方文档:CssProvider --- GNOME Python API

Deprecated since version 4.12: Use load_from_string or load_from_bytes instead

Styling GTK3 in Python with CSS (Shallow Thoughts)

控件对应的CSS选择器名称(Built-in Class Names)

复制代码
css = 'button { background-color: #f00; }'

CSS选择器名称可以改(User-set Class Names)

复制代码
    button_context = button.get_style_context()
    button_context.add_class("whitebutton")

控件名称,映射为 HTML 标签的ID,在CSS中使用井号选择器

(Widget Names, like CSS ID)

复制代码
    label = gtk.Label()
    label.set_use_markup(True)
    label.set_line_wrap(True)
    label.set_name("red_label")
    mainbox.pack_start(label, False, False, 0)
    css = '#red_label { background-color: #f00; }'

dflabel = gtk.Label() 
相关推荐
AI攻城狮1 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽2 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健17 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
球球pick小樱花18 小时前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
码路飞19 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽21 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效