在框架上的“ <Enter>”上调用函数会导致在调用Enter函数时框架闪烁

时间:2019-11-12 01:20:57

标签: python tkinter

当用户将鼠标悬停在框架上时,我正在尝试更改其颜色。我已经成功完成了100万次,但与此必须有所不同,因为每当<Enter><Leave>时,小部件的大小都会增加一秒钟,而不仅仅是改变颜色事件被调用。这会导致有关UI的闪烁效果。

当我将changecolor函数更改为仅更改Frame子代的颜色时,就没有问题了。调用config选项时,是什么导致Frame本身闪烁?


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()

预期结果是,鼠标悬停时,卡片项目将显示为浅蓝色。取而代之的是,在重新绘制之前,它显示为浅蓝色,并且卡闪烁并短暂增大尺寸。 我想念什么?

0 个答案:

没有答案