免费下载app软件网站/黑马培训是正规学校吗
iconphoto()方法用于设置任何tkinter /顶层窗口的标题栏图标。但是要将任何图像设置为标题栏的图标,图像应该是PhotoImage类的对象。
用法:
iconphoto(self, default = False, *args)
设置图标图像的步骤-
from tkinter import Tk
master = Tk()
photo = PhotoImage(file = "Any image file")
master.iconphoto(False, photo)
根据通过的命名照片图像为此窗口设置标题栏图标args。如果default为True,这也将应用于将来创建的所有顶级。图像中的数据在调用时作为快照。如果以后更改图像,则不会在标题栏图标中反映出来。该功能还可以将提供的图标缩放到适当的大小。
代码1:提供PhotoImage时。
# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
# Creating master Tkinter window
master = Tk()
# Creating object of photoimage class
# Image should be in the same folder
# in which script is saved
p1 = PhotoImage(file = 'info.png')
# Setting icon of master window
master.iconphoto(False, 'info.png')
# Creating button
b = Button(master, text = 'Click me !')
b.pack(side = TOP)
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()
输出:
异常:如果直接提供图像而不是PhotoImage对象,则它将显示以下错误。
代码2:未提供PhotoImage对象时。
# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
# Creating master Tkinter window
master = Tk()
# Setting icon of master window
master.iconphoto(False, 'info.png')
# Creating button
b = Button(master, text = 'Click me !')
b.pack(side = TOP)
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()
输出:
Traceback (most recent call last):
File "C:\Users\Admin\Documents\GUI_python\geeks.py", line 14, in
master.iconphoto(False, 'info.png')
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1910, in wm_iconphoto
self.tk.call('wm', 'iconphoto', self._w, *args)
_tkinter.TclError:can't use "info.png" as iconphoto:not a photo image