Tkinter 将图像添加到标题栏上的按钮
Tkinter Adding Images to a Button on a Title Bar
我似乎在将图像添加到我的自定义标题栏时遇到了问题,我似乎无法解决它。我尝试在网上搜索,但无法获得我想要的结果。
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="-", bd=0, height=3, width=5)
maximise_button = tkr.Button(title_bar, text="min", bd=0, height=3, width=5)
menu_button = tkr.Button(title_bar, image=menu_image, bg=Col_bg3, 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, image=menu_image, bg=Col_bg3, fg=Col_fg1, highlightbackground=Col_bg4, 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="-", bd=0, height=3, width=5)
maximise_button = tkr.Button(title_bar, text="min", bd=0, height=3, width=5)
menu_button = tkr.Button(title_bar, image=menu_image, bg=Col_bg3, fg=Col_fg1, highlightbackground=Col_bg4, bd=0)
我似乎在将图像添加到我的自定义标题栏时遇到了问题,我似乎无法解决它。我尝试在网上搜索,但无法获得我想要的结果。
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="-", bd=0, height=3, width=5)
maximise_button = tkr.Button(title_bar, text="min", bd=0, height=3, width=5)
menu_button = tkr.Button(title_bar, image=menu_image, bg=Col_bg3, 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, image=menu_image, bg=Col_bg3, fg=Col_fg1, highlightbackground=Col_bg4, 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="-", bd=0, height=3, width=5)
maximise_button = tkr.Button(title_bar, text="min", bd=0, height=3, width=5)
menu_button = tkr.Button(title_bar, image=menu_image, bg=Col_bg3, fg=Col_fg1, highlightbackground=Col_bg4, bd=0)