如何解决Tkinter 将图像添加到标题栏上的按钮
我似乎在向自定义标题栏添加图像时遇到问题,但似乎无法解决。我尝试在网上搜索,但没有得到我想要的结果。
import tkinter as tkr
from tkinter import *
from tkinter.ttk import *
from PIL import Image
window = tkr.Tk()
window.geometry("1000x500")
window.overrideredirect(1)
title_bar = tkr.Frame(window,bg=Col_bg3,relief='raised',bd=0)
#Title Bar Buttons
close_button = tkr.Button(title_bar,text="✕",bd=0,height=3,width=5)
minimise_button = tkr.Button(title_bar,text="-",width=5)
maximise_button = tkr.Button(title_bar,text="min",width=5)
menu_button = tkr.Button(title_bar,image=menu_image,fg=Col_fg1,highlightbackground=Col_bg4,bd=0)
menu_image = PhotoImage(title_bar,file="images/menu.png")
title_bar.pack(expand=0,fill="x")
close_button.pack(side=tkr.RIGHT)
maximise_button.pack(side=tkr.RIGHT)
minimise_button.pack(side=tkr.RIGHT)
menu_button.pack(side=tkr.LEFT,padx=(50,10))
window.mainloop()
这是控制台给我的:
Traceback (most recent call last):
File "[...]main.py",line 69,in <module>
menu_button = tkr.Button(title_bar,bd=0)
NameError: name 'menu_image' is not defined
Process finished with exit code 1
我仍在学习这门语言,所以我可能以不正确的方式完成了它,但我已经尝试将变量设置为全局变量,将变量设置为 self。但我无法让它工作。有没有人可能知道我如何让这个工作?
解决方法
在您的 menu_bar
对象中引用它之前,您的 menu_button
对象尚未初始化。
先尝试初始化。
#Title Bar Buttons
menu_image = PhotoImage(title_bar,file="images/menu.png")
close_button = tkr.Button(title_bar,text="✕",bd=0,height=3,width=5)
minimise_button = tkr.Button(title_bar,text="-",width=5)
maximise_button = tkr.Button(title_bar,text="min",width=5)
menu_button = tkr.Button(title_bar,image=menu_image,bg=Col_bg3,fg=Col_fg1,highlightbackground=Col_bg4,bd=0)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。