如何使用Tkinter设置窗口图标? [英] How do I setup a window Icon using Tkinter?

查看:854
本文介绍了如何使用Tkinter设置窗口图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个窗口图标,以便它显示在我正在做的词典名称的旁边.运行代码时,出现以下问题:

I'm trying to set up a window Icon so it would appear right next to the name of a dictionary I'm doing. When I run the code, I get the following problem:

Traceback (most recent call last):
  File "/Users/sergioley-languren/Latin_app/windows.py", line 20, in <module>
    window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Latin_app/Logo.jpeg'))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "/Users/sergioley-languren/Latin_app/Logo.jpeg"

当我尝试使用iconbitmap tkinter函数并将.jpeg文件转换为.ico图片时,它起作用了,但是我得到了一个空白页面图标.下面的代码是有问题的代码:

When I tried to use the iconbitmap tkinter function with the .jpeg file converted to a .ico picture, it worked, but I got a blank page icon instead. The code below is the problematic code:

### Imports
import tkinter as tk
from tkinter import *
from pathlib import Path

##Extra Code
home = print(Path.home())
### Application screen code
window = Tk()

##Frame
frame1 = Frame(window)
frame1.pack()

## Window Title
window.title("Latin Unit 1 Dictionary - Based on Orion Academy Latin I , Unit 1")
window.geometry('1110x950')

#App Icon
window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Latin_app/Logo.jpeg'))

## Window Main Loop
window.mainloop()

有人可以告诉我如何解决吗? (如果有帮助,我正在使用MacOs Catilina.)

Could anyone tell me how to fix it? (If it helps, I'm using MacOs Catilina.)

推荐答案

tkinter.PhotoImage仅支持GM, PPM, GIF, PNG图片.您的图片为JPEG,请确保它将引发Exception.

tkinter.PhotoImage only support GM, PPM, GIF, PNG image.Your image is JPEG,for sure it will raise Exception.

有一些解决方案,但是直接的方法是将图像扩展名更改为ico并使用iconbitmap()

There are some solutions,but the direct way is to change the image extension to ico and use iconbitmap()

from PIL import Image

img = Image.open(r"xxx.jpeg") # your jpeg image path
img.save(r"Icon.ico")

这将在您当前的路径中生成一个新的ico图片.

This will generate a new ico image in your current path.

然后您可以直接使用iconbitmap('Icon.ico').

这篇关于如何使用Tkinter设置窗口图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆