如何在 Gnome Shell 中设置应用程序标题?

发布于 2025-01-06 15:45:05 字数 257 浏览 1 评论 0原文

我是 Gtk+ 开发的新手,正在尝试使用 PyGObject 和 Gtk+3.0 编写一个应用程序。然而,当我从命令行在 Gnome Shell 中运行应用程序时,出现在左上角(紧邻“活动”热角右侧)的应用程序名称仅设置为 Python 源文件的名称我跑去启动应用程序。有什么方法可以设置我的应用程序的名称出现在 Gnome Shell 中吗?我查看了 Gtk.Application,虽然它似乎做了一些我想要的事情(无论如何从 Gtk+3.3 开始),但我似乎不知道如何修复活动名称或应用程序名称。

I am new to Gtk+ development, and am trying to write an app using PyGObject and Gtk+3.0. When I run my app in Gnome Shell from the command line, however, the application name as it appears in the upper-left hand corner (immediately to the right of the Activities hot corner) is just set to the name of the Python source file that I ran to start the app. Is there any way to set the name to appear in Gnome Shell for my application? I've looked at Gtk.Application, and though it seems to do some of what I want (starting in Gtk+3.3, anyway), I can't seem to figure out how to fix the activity name or the application name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2025-01-13 15:45:05

gnome-shell 尝试将窗口与应用程序(ShellApp 实例)匹配并使用该名称。执行此操作的代码位于: http://git.gnome .org/browse/gnome-shell/tree/src/shell-window-tracker.c#n328

但是,如果它无法找到窗口的 ShellApp ,那么它会回退到使用ICCCM指定WM_CLASS (规范位于 http://tronche. com/gui/x/icccm/sec-4.html#s-4.1.2.5)这里:http://git.gnome.org/browse/gnome-shell/tree/ src/shell-app.c#n361

因此,如果您没有安装 .desktop 文件来查找应用程序名称,那么您将获得默认的 WM_CLASS 出现在其中。 GTK 根据可执行文件名称自动生成。您可以在实现窗口之前(这意味着在窗口上调用 _show 之前)使用 gtk_window_set_wmclass() 覆盖它。

这是一个简单的示例,将显示为“Hello World” 。不要忘记设置窗口标题!

#!/usr/bin/python
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_wmclass ("Hello World", "Hello World")
win.set_title ("Hello World")
win.show_all()
Gtk.main()

gnome-shell tries to match the window to an an app (a ShellApp instance) and use that name. The code do that is here: http://git.gnome.org/browse/gnome-shell/tree/src/shell-window-tracker.c#n328

But if it fails to find ShellApp for the window then it falls back to using the ICCCM specified WM_CLASS (spec is at http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.2.5) here: http://git.gnome.org/browse/gnome-shell/tree/src/shell-app.c#n361

So if you're not installing a .desktop file for it to find the application name from you'll get the default WM_CLASS appearing in there. GTK automatically generates based on the executable name. You can override that before the window is realized (this means before calling _show on the window) using gtk_window_set_wmclass()

Here is a simple example that will appear as "Hello World". Don't forget to set a window title too!

#!/usr/bin/python
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_wmclass ("Hello World", "Hello World")
win.set_title ("Hello World")
win.show_all()
Gtk.main()
对不⑦ 2025-01-13 15:45:05

2023 年和 GTK4 的答案:set_wmclass 已弃用。要在 Gnome shell 顶部栏中获取自定义名称,您可以使用 set_prgname (C 链接Vala),请注意 set_application_name 不会更改顶栏中的名称。

这是 Vala 中的一个示例:

public class HelloApp : Gtk.Application {
    public const string VERSION = "0.1.0";
    public HelloApp() {
        Object(application_id: "com.example.apps.hello");
    }
    protected override void activate() {
        var window = new Gtk.ApplicationWindow(this) {
            default_width = 640,
            default_height = 480,
            title = @"Hello $(VERSION)"
        };
        window.present();
    }
    public static int main(string[] args) {
        Environment.set_prgname(@"Hello $(VERSION)");
        return new HelloApp().run(args);
    }
}

构建:

valac --target-glib=auto --pkg gtk4 hello.vala -o hello

An answer from 2023 and GTK4: set_wmclass is deprecated. To get a custom name in the Gnome shell topbar you can use set_prgname (link for C, link for Vala), note that set_application_name won't change the name in the topbar.

Here is an example in Vala:

public class HelloApp : Gtk.Application {
    public const string VERSION = "0.1.0";
    public HelloApp() {
        Object(application_id: "com.example.apps.hello");
    }
    protected override void activate() {
        var window = new Gtk.ApplicationWindow(this) {
            default_width = 640,
            default_height = 480,
            title = @"Hello $(VERSION)"
        };
        window.present();
    }
    public static int main(string[] args) {
        Environment.set_prgname(@"Hello $(VERSION)");
        return new HelloApp().run(args);
    }
}

To build:

valac --target-glib=auto --pkg gtk4 hello.vala -o hello
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文