【发布时间】:2019-08-04 17:15:12
【问题描述】:
我正在为数学公式计算器编写 GUI。我想创建多个按钮,在将鼠标悬停在它们上时更改背景图像,但我真的不知道该怎么做...
我已经尝试为按钮本身创建一个类,以便我可以修改它的行为,但它不起作用......
import tkinter as tk
class HoverButton(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self, master=master,**kw)
self.defaultbackground = tk.PhotoImage(file = "GeometricBackground.png")
self.currentbackground = tk.PhotoImage(file = "")
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, currentbackground):
image = tk.Label(self, image = currentbackground)
image.pack()
def on_leave(self):
image = tk.Label(self, image = self.defaultbackground)
image.pack()
root = tk.Tk()
classButton = HoverButton(root, currentbackground = "MainMenu.png")
classButton.grid()
root.mainloop()
我真的希望这会削减它,但执行时我收到了以下错误消息:
_tkinter.TclError: unknown option "-currentbackground"
任何帮助将不胜感激:)
【问题讨论】:
标签: python-3.x tkinter