python - 如何设置 tkinter 窗口的 WM_CLASS 字符串的应用程序名称?

标签 python ubuntu tkinter

简介

为了在桌面启动器(我使用的是 Ubuntu 17.04)中将给定应用程序的多个实例分组到一个图标下,它们必须具有相同的 WM_CLASS 字符串的 appName 属性。例如,如果我运行 emacs 两次:

$ emacs &
$ emacs &

这两个实例都将显示在桌面启动栏中的 Emacs 图标下。原因是两个实例都具有相同的 WM_CLASS 字符串。我们可以使用

检查这个字符串
$ xprop WM_CLASS

然后单击 Emacs 窗口。然后显示:

WM_CLASS(STRING) = "emacs", "Emacs"

这里“emacs”是资源(appName),“Emacs”是className, 见xdotool: what are “class” and “classname” for a window?获取更多信息。

问题

考虑这个程序(my-tkapp.py):

#! /usr/bin/env python
import tkinter as tk
root = tk.Tk(className='myTkApp')
label = tk.Label(root, text="Hello World")
label.pack()
root.mainloop()

如果我运行这个程序两次:

$ my-tkapp.py &
$ my-tkapp.py &

然后运行xprop检查两个窗口的WM_CLASS属性, 第一个窗口给出:

WM_CLASS(STRING) = "myTkApp", "Mytkapp"

而第二个给出:

WM_CLASS(STRING) = "myTkApp #2", "Mytkapp"

请注意,tkinter 已将 #2 后缀添加到应用名称属性。这是不希望的。它使窗口管理器将两个窗口分组在桌面启动栏中的单独图标下。

如何为应用程序的不同实例保留 WM_CLASS 字符串的相同 appName 属性?

另见

How to add launcher icon for python script?

最佳答案

这是默认情况下的 tkinters 行为。 className 必须是唯一的才能与 tkinter.send 一起使用并记录为 appname .

If newName is not specified, this command returns the name of the application (the name that may be used in send commands to communicate with the application). If newName is specified, then the name of the application is changed to newName. If the given name is already in use, then a suffix of the form “ #2” or “ #3” is appended in order to make the name unique. The command's result is the name actually chosen. newName should not start with a capital letter. This will interfere with option processing, since names starting with capitals are assumed to be classes; as a result, Tk may not be able to find some options for the application. If sends have been disabled by deleting the send command, this command will reenable them and recreate the send command.

使用wm_group反而。另见 Q&A for dialog windows

关于python - 如何设置 tkinter 窗口的 WM_CLASS 字符串的应用程序名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795622/

相关文章:

python - 如何在 Tkinter 中使用 "native"GUI 外观?

python - 卡尔达诺的公式不适用于 numpy?

java - 为什么我不能在 Ubuntu 12.04 中设置 JAVA_HOME 变量?

python - 如何查找 **options 参数是什么?

python - 验证 Tkinter.Text 小部件?

linux - wget 不从真实的 https url vaultpress 下载

python JIRA 与代理的连接

python:为类对象设置只读属性

python - 在不正确的 merge 之前了解用于硬重置的pycharm提交日志

linux - 搜索和复制文件时添加检查文件是否存在的功能