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. 但我无法让它工作。有谁可能知道我怎样才能让这个工作?

4

1 回答 1

-1

在您在对象menu_bar中引用它之前,您的对象尚未初始化menu_button

尝试先初始化它。

#Title Bar Buttons
menu_image = PhotoImage(title_bar, file="images/menu.png")

close_button = tkr.Button(title_bar, text="✕&quot;, 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)

于 2020-12-30T05:59:25.063 回答