如何在 Ubuntu 面板中更改 Tkinter 应用程序显示名称?

问题描述 投票:0回答:2

我使用 Tkinter 在 Ubuntu 中制作了一个文本编辑器,并使用

wm_title()
函数更改名称。它在标题栏中发生了变化,但是当我将鼠标悬停在 Ubuntu 面板中它的图标上时,它的名称显示为
Tk

查看屏幕截图这里

完整代码:

from tkinter import *
import tkinter.filedialog as fd
from tkinter.scrolledtext import ScrolledText
import platform
filepath = ""

def setfilepath(path):
    global filepath
    filepath = path

def openfile():
    path = fd.askopenfilename()
    if not path==():
        with open(path, 'r') as f:
            code = f.read()
            editor.delete('1.0', END)
            editor.insert("1.0", code)
            setfilepath(path)

def savefile():
    if filepath == "":
        saveasfile()
    else:
        with open(filepath, 'w') as f:
            code = editor.get('1.0', END)
            f.write(code)

def saveasfile():
    path = fd.asksaveasfilename()
    if not path==():
        with open(path, 'w') as f:
            code = editor.get('1.0', END)
            f.write(code)
            setfilepath(path)

root = Tk()
root.wm_title("Tedit")
editor = ScrolledText(wrap=WORD, undo=True)
editor.pack(fill=BOTH, expand=True)
menubar = Menu(root)
fileMenu = Menu(menubar, tearoff=False)
fileMenu.add_command(label="Open", command=openfile)
fileMenu.add_command(label="Save", command=savefile)
fileMenu.add_command(label="Save As", command=saveasfile)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menubar, tearoff=False)
editMenu.add_command(label="Cut", accelerator="Ctrl+X", command=lambda: editor.event_generate('<<Cut>>'))
editMenu.add_command(label="Copy", accelerator="Ctrl+C", command=lambda: editor.event_generate('<<Copy>>'))
editMenu.add_command(label="Paste", accelerator="Ctrl+V", command=lambda: editor.event_generate('<<Paste>>'))
editMenu.add_separator()
editMenu.add_command(label="Undo", accelerator="Ctrl+Z", command=editor.edit_undo)
editMenu.add_command(label="Redo", accelerator="Ctrl+Shift+Z", command=editor.edit_redo)
menubar.add_cascade(label="File", menu=fileMenu)
menubar.add_cascade(label="Edit", menu=editMenu)
root.config(menu=menubar)
root.bind()
root.mainloop()
python python-3.x tkinter
2个回答
1
投票

@j_4321 帮我回答这个问题。给他额外的额外荣誉。 新代码:

from tkinter import *
import tkinter.filedialog as fd
from tkinter.scrolledtext import ScrolledText
import platform
filepath = ""

def setfilepath(path):
    global filepath
    filepath = path

def openfile():
    path = fd.askopenfilename()
    if not path==():
        with open(path, 'r') as f:
            code = f.read()
            editor.delete('1.0', END)
            editor.insert("1.0", code)
            setfilepath(path)

def savefile():
    if filepath == "":
        saveasfile()
    else:
        with open(filepath, 'w') as f:
            code = editor.get('1.0', END)
            f.write(code)

def saveasfile():
    path = fd.asksaveasfilename()
    if not path==():
        with open(path, 'w') as f:
            code = editor.get('1.0', END)
            f.write(code)
            setfilepath(path)

root = Tk(className="Tedit")
root.title("Tedit")
editor = ScrolledText(wrap=WORD, undo=True)
editor.pack(fill=BOTH, expand=True)
menubar = Menu(root)
fileMenu = Menu(menubar, tearoff=False)
fileMenu.add_command(label="Open", command=openfile)
fileMenu.add_command(label="Save", command=savefile)
fileMenu.add_command(label="Save As", command=saveasfile)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menubar, tearoff=False)
editMenu.add_command(label="Cut", accelerator="Ctrl+X", command=lambda: editor.event_generate('<<Cut>>'))
editMenu.add_command(label="Copy", accelerator="Ctrl+C", command=lambda: editor.event_generate('<<Copy>>'))
editMenu.add_command(label="Paste", accelerator="Ctrl+V", command=lambda: editor.event_generate('<<Paste>>'))
editMenu.add_separator()
editMenu.add_command(label="Undo", accelerator="Ctrl+Z", command=editor.edit_undo)
editMenu.add_command(label="Redo", accelerator="Ctrl+Shift+Z", command=editor.edit_redo)
menubar.add_cascade(label="File", menu=fileMenu)
menubar.add_cascade(label="Edit", menu=editMenu)
root.config(menu=menubar)
root.bind()
root.mainloop()

谢谢@j_4321


0
投票

这个解决方案对我有用:

root = tk.Tk(className="MY APP'S NAME")
© www.soinside.com 2019 - 2024. All rights reserved.