Я пытаюсь изменить цвет рамки и ее компонентов, когда пользователь наводит на них курсор. Я делал это миллион раз раньше с успехом, но что-то должно отличаться от этого, потому что вместо простого изменения цвета размер виджета увеличивается на долю секунды при вызове событий <Enter>
или <Leave>
. Это приводит к мерцающему эффекту в интерфейсе пользователя.
Когда я меняю свои функции изменения цвета, чтобы изменить только цвет дочерних элементов кадра, тогда у меня нет проблем. Что вызывает мерцание самого кадра при вызове параметра config?
import tkinter as tk
from tkinter import *
import time
class InfoCard(Frame):
'''A class that displays a value and a caption in a aesthetic manner, on a rectangle.
A command can be passed that is called on click. If the card has an associated command, the card will respond to hover input. '''
def __init__(self, parent=None, *, background=None, foreground=None, labelforeground=None, labeltext=None, value=None, command=None):
self.parent = parent
self.master = parent
self.main = self.master
self.background=background
self.foreground=foreground
self.labelforeground=labelforeground
self.labeltext=labeltext
self.value=value
self.command=command
Frame.__init__(self, parent)
self.config(bg = self.background, bd=0, highlightthickness=0, highlightcolor=self.background, highlightbackground=self.background)
self.frame=Frame(self, bg=self.background, relief=FLAT, bd=0, highlightthickness=0)
self.frame.grid(row=0, column=0, sticky='nsew', padx=0, pady=0)
self.valuelabel = Label(self.frame, text = str(self.value), font = ("Segoe UI Light", 28, "normal"), relief=FLAT)
self.valuelabel.grid(row=1, column=0, sticky='ew', padx=5, pady=(5, 0))
self.valuelabel.config(foreground=self.foreground, background=self.background, bd=0, highlightthickness=0)
self.textlabel=Label(self.frame, text = str(labeltext), font = ("Segoe UI", 14, "normal"), relief=FLAT)
self.textlabel.grid(row=2, column=0, sticky='ew', padx=(5, 5), pady=(0, 5))
self.textlabel.config(foreground=self.labelforeground, background=self.background, bd=0, highlightthickness=0)
self.frame.columnconfigure(0, weight=1)
self.frame.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.bind('<Button-1>', self.click)
self.valuelabel.bind('<Button-1>', self.click)
self.textlabel.bind('<Button-1>', self.click)
self.bind("<Enter>", self.hover)
self.bind("<Leave>", self.leave)
if self.command != None:
self.config(cursor="hand2")
def click(self, event):
if self.command != None:
self.command()
def hover(self, event):
#print('called hover')
if self.command != None:
self.changecolor('light blue')
def leave(self, event):
#print('called leave')
if self.command != None:
self.leavechangecolor(self.background)
def changecolor(self, color):
#print('tried to print')
#if i leave the next line in the flickering will occur
self.config(bg=color)
self.frame.config(bg=color)
self.textlabel.config(bg=color)
self.valuelabel.config(bg=color)
def leavechangecolor(self, color):
self.config(bg=color)
self.frame.config(bg=color)
self.textlabel.config(bg=color)
self.valuelabel.config(bg=color)
def main():
root = Tk()
#root.geometry('%dx%d' % (200, 100))
mycard=InfoCard(root, background='white', foreground='red', labelforeground='blue', labeltext="Total Columns", value=12)
mycard.grid(row=1, column=1, sticky='ew', padx=50, pady=25)
yourcard=InfoCard(root, background='white', foreground='red', labelforeground='blue', labeltext="Table Rows", value="5,200")
yourcard.grid(row=1, column=2, sticky='ew', padx=50, pady=25)
theircard=InfoCard(root, background='white', foreground='red', labelforeground='blue', labeltext="Columns with Blank Values", value=1, command=printselected)
theircard.grid(row=1, column=3, sticky='ew', padx=50, pady=25)
root.columnconfigure(3, weight=1, minsize=300)
root.update()
root.mainloop()
def printselected():
print('the command was called by clicking')
if __name__ == main():
main()
Ожидаемый результат заключается в том, что при наведении курсора элемент карты будет светло-голубым. Вместо этого он светится синим цветом, а карта мигает и кратковременно увеличивается в размерах, прежде чем перерисовывается. Чего мне не хватает?