python - 如何使用 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()
当我添加这个时,设计全乱了,我不知道为什么。如果你注释掉这个按钮,你会看到我设计的页面被这个搞砸了。有谁知道任何修复?
解决方案
它不起作用的原因是因为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()
图片供参考:
希望这可以帮助!
推荐阅读
- c++ - 无法在我的 .cpp 文件中正确使用命名空间
- python - 熊猫 Python 问题
- c++ - uwebsockets 无法启动:libuv.c:-1: 错误:未定义对 `uv_is_closing' 的引用
- python - 有没有办法将类对象名称转换为与之关联的字符串?
- amazon-web-services - 仅向一位 IAM 用户授予对 Amazon S3 存储桶的访问权限
- laravel - 如何为数据透视表创建测试?
- wso2 - 如何更改 WSO2 3.2.0 APIM 的默认端口?
- firebase - firestore 更新地图字段
- apache-kafka - KSQLDB - 流不使用 TOPIC 键产生任何结果
- javascript - 在第一页加载时反应不准确地计算 div 的高度