Python tkinter错误设置图标,TclError linux

Python tkinter错误设置图标,TclError linux,python,ubuntu,tkinter,icons,Python,Ubuntu,Tkinter,Icons,我对python相当陌生,对GUI也非常陌生,所以如果我犯了一个明显的错误,请告诉我 这也是我的第一篇帖子,所以如果我需要以某种方式编辑我的问题,请让我知道 我不明白为什么在尝试将图标设置到tkinter GUI时会出错。 我已经尝试了以下帖子: 关于这个主题还有其他的stack Exunge帖子,但它们是针对windows和/或他们的OP的,通常可以将图标设置为apear,一旦图标不起作用。这不是我的情况,程序无法运行。虽然我也尝试过他们的一些建议 我尝试了三种不同的编码方式: 方法1:

我对python相当陌生,对GUI也非常陌生,所以如果我犯了一个明显的错误,请告诉我

这也是我的第一篇帖子,所以如果我需要以某种方式编辑我的问题,请让我知道

我不明白为什么在尝试将图标设置到tkinter GUI时会出错。 我已经尝试了以下帖子:

关于这个主题还有其他的stack Exunge帖子,但它们是针对windows和/或他们的OP的,通常可以将图标设置为apear,一旦图标不起作用。这不是我的情况,程序无法运行。虽然我也尝试过他们的一些建议

我尝试了三种不同的编码方式: 方法1:

from tkinter import *
import os

class MainWindow:
    
    def __init__(self, master):
        self.master = master
        master.title('TD')
        #self.iconbitmap(default = 'fist.ico')

root = Tk()
root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))
my_gui = MainWindow(root)
root.mainloop()
结果:

runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 23, in <module>
    root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2116, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, "-default", *args)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.png" as iconphoto: not a photo image
In [43]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 22, in <module>
    root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2071, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)

TclError: bitmap "~./envs/thermTD_project/thermTD/favicon-3.ico" not defined
In [45]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 21, in <module>
    root.tk.call('wm','iconphoto', root._w, img)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.ico" as iconphoto: not a photo image
方法2:

from tkinter import *
import os

class MainWindow:
    
    def __init__(self, master):
        self.master = master
        master.title('TD')
        #self.iconbitmap(default = 'fist.ico')

root = Tk()

root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))
my_gui = MainWindow(root)
root.mainloop()
结果:

runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 23, in <module>
    root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2116, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, "-default", *args)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.png" as iconphoto: not a photo image
In [43]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 22, in <module>
    root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2071, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)

TclError: bitmap "~./envs/thermTD_project/thermTD/favicon-3.ico" not defined
In [45]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 21, in <module>
    root.tk.call('wm','iconphoto', root._w, img)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.ico" as iconphoto: not a photo image
方法3:

from tkinter import *
import os

class MainWindow:
    
    def __init__(self, master):
        self.master = master
        master.title('TD')
        #self.iconbitmap(default = 'fist.ico')

root = Tk()
img = PhotoImage(os.path.join(os.getcwd(),'favicon-3.ico'))
root.tk.call('wm','iconphoto', root._w, img)
my_gui = MainWindow(root)
root.mainloop()
结果:

runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 23, in <module>
    root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2116, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, "-default", *args)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.png" as iconphoto: not a photo image
In [43]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 22, in <module>
    root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))

  File "/usr/lib/python3.8/tkinter/__init__.py", line 2071, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)

TclError: bitmap "~./envs/thermTD_project/thermTD/favicon-3.ico" not defined
In [45]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):

  File "~./envs/thermTD_project/thermTD/untitled2.py", line 21, in <module>
    root.tk.call('wm','iconphoto', root._w, img)

TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.ico" as iconphoto: not a photo image
[45]中的
:运行文件('~./envs/thermTD_项目/thermTD/untitled2.py',wdir='~./envs/thermTD_项目/thermTD')
回溯(最近一次呼叫最后一次):
文件“~./envs/thermTD_project/thermTD/untitled2.py”,第21行,在
root.tk.call('wm','iconphoto',root.\w,img)
TclError:不能将“~./envs/thermTD_project/thermTD/favicon-3.ico”用作iconphoto:不是照片图像
文件存在性检查: 见方法1

没有任何图标设置行的代码按预期工作

我尝试过在每种方法上使用gif、png和ico。我正在尝试其他文件类型

注意:虽然文件名是favicon,但实际上它们是64x64像素的ico、gif、png文件。即使favicon ico也只有一种图标(如果这是正确的话)。只是我从favicon那里得到的,我用mogrify转换成了不同的格式

操作系统:Ubuntu 20.04.1 LTS python 3.8.2 IDE:spyder 4.1.5


另外,如果我能知道在我的init函数中包含此函数的语法,我将不胜感激。我正在尝试使用面向对象的GUI,我仍在学习。

您是否尝试过使用ico作为图标,只有那些图标通过
iconbitmap()
方法受支持,否则使用png,请看一看,是的,我刚刚尝试过获得[TclError:bitmap”~./envs/thermTD\u project/thermTD/favicon-3.ico“未定义”。发布后,我一直在仔细检查每种方法的每种文件类型是否对我不起作用。现在转到bmp。。。谢谢你的评论。你是在用文件转换器把文件从一个扩展名改成另一个扩展名吗?重命名文件只会使其损坏是的,我正在ubuntu上使用mogrify切换格式。我可以在Ubuntu的图像查看器中打开我正在加载的每个文件,这样我就不会认为它们已损坏。
PhotoImage(…)
应该是
PhotoImage(file=…)