鼠标悬停时无法更改 tkinter Canvas 背景颜色?
Unable to change tkinter Canvas background color on mouse hover?
我想在鼠标悬停时更改完整 canvas 的背景颜色。有这个代码。最小示例:
import tkinter as tk
class HoverCanvas(tk.Canvas):
def __init__(self, master, activebackground, **kw):
tk.Frame.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.activebackground = activebackground
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, e):
self.config(background=self.activebackground)
def on_leave(self, e):
self.config(background=self.defaultBackground)
root = tk.Tk()
root.geometry("1280x720")
canvas = HoverCanvas(root, 'red', bg='#212121', width=1280, height=720)
#canvas = tk.Canvas(root, bg='#212121', width=1280, height=720)
canvas.create_text(110, 15, fill="#304ffe", activefill='#6a1b9a', font="Times 14 bold", text="Soccer Data Scraper v1.0")
canvas.grid(row=0, column=0)
root.mainloop()
canvas 的颜色在鼠标指向它时变为红色,如果它是空的(见注释行)。
但是,如果我尝试在 canvas 上添加文本或任何其他小部件,程序将停止工作并抛出一个神秘错误。
Traceback (most recent call last):
File "canvasbg.py", line 24, in <module>
canvas.create_text(110, 15, fill="#304ffe", activefill='#6a1b9a', font="Times 14 bold", text="Soccer Data Scraper v1.0")
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 2501, in create_text
return self._create('text', args, kw)
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 2477, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad option "create": must be cget or configure
这是什么错误?有什么方法可以在光标悬停时更改 canvas 的背景颜色,同时还包含其他 widgets/text 吗?
感谢任何帮助。谢谢
您的 class 继承自 tk.Canvas
class HoverCanvas(tk.Canvas):
但是你调用了tk.Frame
的__init__
方法
tk.Frame.__init__(self,master=master,**kw)
您应该改为调用 tk.Canvas
的 __init__
方法。
我想在鼠标悬停时更改完整 canvas 的背景颜色。有这个代码。最小示例:
import tkinter as tk
class HoverCanvas(tk.Canvas):
def __init__(self, master, activebackground, **kw):
tk.Frame.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.activebackground = activebackground
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, e):
self.config(background=self.activebackground)
def on_leave(self, e):
self.config(background=self.defaultBackground)
root = tk.Tk()
root.geometry("1280x720")
canvas = HoverCanvas(root, 'red', bg='#212121', width=1280, height=720)
#canvas = tk.Canvas(root, bg='#212121', width=1280, height=720)
canvas.create_text(110, 15, fill="#304ffe", activefill='#6a1b9a', font="Times 14 bold", text="Soccer Data Scraper v1.0")
canvas.grid(row=0, column=0)
root.mainloop()
canvas 的颜色在鼠标指向它时变为红色,如果它是空的(见注释行)。 但是,如果我尝试在 canvas 上添加文本或任何其他小部件,程序将停止工作并抛出一个神秘错误。
Traceback (most recent call last):
File "canvasbg.py", line 24, in <module>
canvas.create_text(110, 15, fill="#304ffe", activefill='#6a1b9a', font="Times 14 bold", text="Soccer Data Scraper v1.0")
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 2501, in create_text
return self._create('text', args, kw)
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 2477, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad option "create": must be cget or configure
这是什么错误?有什么方法可以在光标悬停时更改 canvas 的背景颜色,同时还包含其他 widgets/text 吗?
感谢任何帮助。谢谢
您的 class 继承自 tk.Canvas
class HoverCanvas(tk.Canvas):
但是你调用了tk.Frame
__init__
方法
tk.Frame.__init__(self,master=master,**kw)
您应该改为调用 tk.Canvas
的 __init__
方法。