![](/img/trans.png)
[英]how to make this function work in order with the tkinter label
[英]How to make an image work inside a function in tkinter
嘿,我在 function 中有一个图像,它没有出现在它应该出现的位置。 图像与代码保存在同一文件夹中。 查看解决方案后,我发现我必须将图像设为全局变量才能使其工作。 我这样做了,在它再次不起作用之后,我尝试将其他一些变量设为全局变量,因为我不确定我是否对正确的变量进行了操作。
我还想知道问题是否可能是由于它位于 function 中而导致几何无法正常工作
这是它应该是什么样子(左上角主页按钮,主页图标是图像) 这就是它在 function 内的结果
如果你想看的话,这里是 function
from tkinter import *
def resourcePage():
global homeIcon
global file
resource = Tk()
resource.title("Resource Page")
resource.resizable(0, 0)
header = LabelFrame(resource, bg="white")
content = LabelFrame(resource, bg="white")
header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space
homeButton = Button(content, width=50, height=50)
try:
homeIcon = PhotoImage(file="yes.png")
homeButton.config(image=homeIcon)
homeButton.image = homeIcon
except TclError:
print("Home")
homeButton.grid(row=1, column=0, sticky="w", padx=2, pady=2)
# each section of code below is for their respective labels on the page
papersLabel = Label(content, text="Exam Papers", bg="#12a8e3", fg="white", font=("Ariel", 26, "bold"),
activebackground="#12a8e3", anchor="w", padx=15)
papersLabel.grid(row=2, column=0, padx=15, pady=(15, 5), ipadx=429, ipady=10)
papersPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
papersPhysics.grid(row=3, column=0, sticky="w", padx=20)
papersHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
papersHyperlinks.grid(row=4, column=0, sticky="w", padx=24)
papersCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
papersCS.grid(row=5, column=0, sticky="w", padx=20)
papersHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
papersHyperlinks.grid(row=6, column=0, sticky="w", padx=24)
questionsLabel = Label(content, text="Practice Exam Questions:", bg="#12a8e3", fg="white",
font=("Ariel", 26, "bold"), activebackground="#12a8e3", anchor="w", padx=15)
questionsLabel.grid(row=7, column=0, padx=15, pady=(25, 5), ipadx=328, ipady=10)
questionsPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
questionsPhysics.grid(row=8, column=0, sticky="w", padx=20)
questionsHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
questionsHyperlinks.grid(row=9, column=0, sticky="w", padx=24)
questionsCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
questionsCS.grid(row=10, column=0, sticky="w", padx=20)
questionsHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
questionsHyperlinks.grid(row=11, column=0, sticky="w", padx=24)
videosLabel = Label(content, text="Helpful Videos:", bg="#12a8e3", fg="white", font=("Ariel", 26, "bold"),
activebackground="#12a8e3", anchor="w", padx=15)
videosLabel.grid(row=12, column=0, padx=15, pady=(25, 5), ipadx=415, ipady=10)
videosPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
videosPhysics.grid(row=13, column=0, sticky="w", padx=20)
videosHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
videosHyperlinks.grid(row=14, column=0, sticky="w", padx=24)
videosCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
videosCS.grid(row=15, column=0, sticky="w", padx=20)
videosHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
videosHyperlinks.grid(row=16, column=0, sticky="w", padx=24,
pady=(0, 25)) # y padding here to keep some space from the bottom of the page
header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')
正如评论中所指出的,来自引发TclError
异常时的消息被抑制。
尝试更改except
子句
except TclError as Error:
print(Error)
对于我运行您的代码,似乎yes.png
无法在运行您的脚本的 Python 进程的工作目录中找到,因为我不断收到
couldn't open "yes.png": no such file or directory
使用占位符test.png
获得的结果:
from tkinter import *
def resourcePage():
global homeIcon
global file
resource = Tk()
resource.title("Resource Page")
resource.resizable(0, 0)
header = LabelFrame(resource, bg="white")
content = LabelFrame(resource, bg="white")
header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space
homeButton = Button(content, width=50, height=50)
try:
#Modification 1:
# Either provide a full or relative path or
# change the working directory by import os; os.chdir()
homeIcon = PhotoImage(file="X:/SomeFolder/yes.png")
homeButton.config(image=homeIcon)
homeButton.image = homeIcon
#Modification 2:
except TclError as Error:
print(Error)
homeButton.grid(row=1, column=0, sticky="w", padx=2, pady=2)
# each section of code below is for their respective labels on the page
papersLabel = Label(content, text="Exam Papers", bg="#12a8e3", fg="white", font=("Ariel", 26, "bold"),
activebackground="#12a8e3", anchor="w", padx=15)
papersLabel.grid(row=2, column=0, padx=15, pady=(15, 5), ipadx=429, ipady=10)
papersPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
papersPhysics.grid(row=3, column=0, sticky="w", padx=20)
papersHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
papersHyperlinks.grid(row=4, column=0, sticky="w", padx=24)
papersCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
papersCS.grid(row=5, column=0, sticky="w", padx=20)
papersHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
papersHyperlinks.grid(row=6, column=0, sticky="w", padx=24)
questionsLabel = Label(content, text="Practice Exam Questions:", bg="#12a8e3", fg="white",
font=("Ariel", 26, "bold"), activebackground="#12a8e3", anchor="w", padx=15)
questionsLabel.grid(row=7, column=0, padx=15, pady=(25, 5), ipadx=328, ipady=10)
questionsPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
questionsPhysics.grid(row=8, column=0, sticky="w", padx=20)
questionsHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
questionsHyperlinks.grid(row=9, column=0, sticky="w", padx=24)
questionsCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
questionsCS.grid(row=10, column=0, sticky="w", padx=20)
questionsHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
questionsHyperlinks.grid(row=11, column=0, sticky="w", padx=24)
videosLabel = Label(content, text="Helpful Videos:", bg="#12a8e3", fg="white", font=("Ariel", 26, "bold"),
activebackground="#12a8e3", anchor="w", padx=15)
videosLabel.grid(row=12, column=0, padx=15, pady=(25, 5), ipadx=415, ipady=10)
videosPhysics = Label(content, text="Physics:", bg="white", font=("Ariel", 22), anchor="w")
videosPhysics.grid(row=13, column=0, sticky="w", padx=20)
videosHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
videosHyperlinks.grid(row=14, column=0, sticky="w", padx=24)
videosCS = Label(content, text="Computer Science:", bg="white", font=("Ariel", 22), anchor="w")
videosCS.grid(row=15, column=0, sticky="w", padx=20)
videosHyperlinks = Label(content, text="• Hyperlinks here", bg="white", font=("Ariel", 18), anchor="w")
videosHyperlinks.grid(row=16, column=0, sticky="w", padx=24,
pady=(0, 25)) # y padding here to keep some space from the bottom of the page
header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')
#Modification 3
resource.mainloop()
resourcePage()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.