首页 > 解决方案 > 如何使用 tkinter 上的图标图像制作主页按钮?

问题描述

我尝试了几种方法来完成此操作,但到目前为止都没有奏效。这是我试图制作的页面设计,并注意到左上角的主页按钮是我遇到的问题。

这是我最近尝试的代码:

from tkinter import *

home = Tk()
home.title("Home Page")
home.resizable(0,0)

header = LabelFrame(home, bg="#12a8e3")
content = LabelFrame(home, bg="white")

header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space

homeButton=Button(header,width=80,height=200)
try:
    homeIcon=PhotoImage(file="font-awesome-computer-icons-house-font-address.jpg")
    homeButton.config(image=homeIcon)
    homeButton.image = image
except TclError:
    pass
homeButton.pack(side=LEFT)

papersLabel = Label(content, text="Exam Papers", padx=430, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
papersLabel.grid(row=1, column=0, columnspan=3, padx=15, pady=40)
papersPhysics = Label(content, text="Physics")
papersPhysics.grid(row=2, column=0)

practiceLabel = Label(content, text="Practice exam questions", padx=341, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
practiceLabel.grid(row=3, column=0, columnspan=3, padx=15, pady=40)

videoLabel = Label(content, text="Helpful videos", padx=421, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
videoLabel.grid(row=4, column=0, columnspan=3, padx=15, pady=40)

header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')

home.mainloop()

当我添加这个时,设计全乱了,我不知道为什么。如果你注释掉这个按钮,你会看到我设计的页面被这个搞砸了。有谁知道任何修复?

如果需要,这是主页图标的图像

标签: pythontkinter

解决方案


它不起作用的原因是因为PhotoImage()仅适用于.png和其他格式,而不是.jpg.

因此,我将其转换为 a .png,代码如下:

from tkinter import *

home = Tk()
home.title("Home Page")
home.resizable(0,0)

header = LabelFrame(home, bg="#12a8e3")
content = LabelFrame(home, bg="white")

header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space

homeButton=Button(header,width=80,height=200)
try:
    homeIcon=PhotoImage(file="yes.png")
    homeButton.config(image=homeIcon)
    homeButton.image = homeIcon

except TclError:
    print("here")
homeButton.pack(side=LEFT)

papersLabel = Label(content, text="Exam Papers", padx=430, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
papersLabel.grid(row=1, column=0, columnspan=3, padx=15, pady=40)
papersPhysics = Label(content, text="Physics")
papersPhysics.grid(row=2, column=0)

practiceLabel = Label(content, text="Practice exam questions", padx=341, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
practiceLabel.grid(row=3, column=0, columnspan=3, padx=15, pady=40)

videoLabel = Label(content, text="Helpful videos", padx=421, pady=15, bg="#12a8e3", fg="white", font=("Ariel",25, "bold"), activebackground="#12a8e3", anchor="w", justify="left")
videoLabel.grid(row=4, column=0, columnspan=3, padx=15, pady=40)

header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')

home.mainloop()

图片供参考:

在此处输入图像描述

希望这可以帮助!


推荐阅读