Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Tkinter条目控件颜色褪色_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x Tkinter条目控件颜色褪色

Python 3.x Tkinter条目控件颜色褪色,python-3.x,tkinter,Python 3.x,Tkinter,在我的新应用程序中,我希望鼠标在entry()小部件上时改变颜色(我知道怎么做),但我希望颜色逐渐改变,而不是立即改变 这是我的代码: # User_Line Focus In/Out def User_Line_Focus_In(self, event): self.User_Line.configure(bg = "#DCDCDC") def User_Line_Focus_Out(self, event): self.User_Line.c

在我的新应用程序中,我希望鼠标在entry()小部件上时改变颜色(我知道怎么做),但我希望颜色逐渐改变,而不是立即改变

这是我的代码:

# User_Line Focus In/Out
    def User_Line_Focus_In(self, event):
        self.User_Line.configure(bg = "#DCDCDC")

    def User_Line_Focus_Out(self, event):
        self.User_Line.configure(bg = "#FFFFFF")

您需要创建一个增加颜色的方法,并且需要使用tkinter的方法来注册在给定时间后调用的报警回调。然后,您需要递归地引用它,以获得所需的淡入淡出效果

    def incrementHex(hex_str, increment): #with hex_str in format "#FFFFFF" or any colour
        red = int(hex_str[1:3],16) #specifies base for integer conversion
        green = int(hex_str[3:5],16)
        blue = int(hex_str[5:],16)
        red += increment #increment can be negative
        green += increment
        blue += increment
        new_hex_str = "#" + str(hex(red)) + str(hex(blue)) + str(hex(green))
        return new_hex_str

    def Fade(self, start_hex, increment):
        new_hex = self.incrementHex(start_hex, increment)
        self.User_Line.configure(bg = new_hex)
        #where self.master is the parent widget as defined in the __init__ method...
        self.master.after(50,lambda: self.Fade(new_hex, increment)) #or any time interval in milliseconds
        #you'll probably need some code to stop it fading here, but I'll let you tackle that one :)

    def User_Line_Focus_In(self, event):
        self.Fade("#FFFFFF",-1) #could be any colour and increment

我还没能测试它,但我认为它原则上应该可以工作。这项技术的一个扩展是对红色、绿色和蓝色有不同的增量。

我认为你必须在这项技术上下功夫并进行编码(tkinter没有内置此功能)

因此,您需要的是:

  • 一种从一种颜色到二种颜色并得到中间颜色的算法。(十六进制值只是以16为基数的数字,但它们可以像普通数字一样加减)
最简单的解决方案是只运行算法(这里的色差)

def淡入淡出颜色(事件,新颜色):
old_color=event.widget.cget('bg')
对于色差中的颜色(旧颜色、新颜色):
event.widget.configure(颜色)
睡眠时间(0.1)
widget.bind(“”,lambda事件:淡入颜色(事件,颜色))
  • 如果用户离开小部件,您可能还希望取消该操作。看一看

<>如果你发现你的GUI在衰落期间变得不响应,你可以考虑使用后方法,你可以在Python和Tkter上读取非阻塞GUI技术的帖子。如果用户离开小部件后立即取消回调(从而释放tkinter来处理其他操作),则这可能不是问题。

当tkinter处于
mainloop
时,决不能使用
时间。睡眠
,因为这会停止运行mainloop,意味着与窗口的交互停止。Tkinter有一个
after
方法来替换我在回答中使用的方法。我如何在RGB比例中进行相同的转换?@KotropulosLeontios如果你的意思是分别处理红色、绿色和蓝色,请更改递增十六进制函数,一次只处理一种颜色,将字符串切片放在incrementHex函数之外,并将其放在Fade函数中。将三个增量值解析为淡入淡出,以便可以分别处理每种颜色。
def fade_colors(event, new_color):
    old_color = event.widget.cget('bg')       
    for color in color_difference(old_color, new_color):
        event.widget.configure(color)
        time.sleep(0.1)

widget.bind('<Enter>', lambda event: fade_colors(event, color))