大佬教程收集整理的这篇文章主要介绍了如何使 ttk 小部件在悬停时缓慢发光,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让我的 ttk
小部件在像 messageBox
OK 按钮一样悬停时缓慢发光。
在您的代码编辑器中键入此内容,然后将鼠标悬停在“确定”按钮上,以了解小部件缓慢发光的含义。
from tkinter import *
from tkinter import messageBox
messageBox.showinfo("","Hover over OK button!")
但是,ttk
小部件在悬停时会立即亮起。
from tkinter import *
from tkinter import ttk
root = Tk()
ttk.button(root,text ="Hover over me!").pack()
root.mainloop()
我可以知道如何让我的 ttk
小部件在悬停时缓慢发光吗?
使用 ttk
您必须创建一堆样式并循环使用它们。那将是荒谬的,也是不必要的。使用 tk
您可以即时更改背景(或任何其他属性)。它更适合您想要的行为。
您不必使用我的 deque
方法。您可以继续增加一些整数并重置它。您还可以执行以下操作:self.colors.append(self.colors.pop(0))
~ 作为代理,这与 deque
所做的基本相同。这里真正的重点是 after
用于继续运行一个方法,该方法在颜色列表中循环,并将当前颜色应用于按钮。
import tkinter as tk
from collections import deque
class GlowButton(tk.button):
def __init__(self,master,**kwargs):
tk.button.__init__(self,**kwargs)
#store BACkground color
self.bg_idle = self.cget('BACkground')
#list of glow colors to cycle through
self.colors = ['#aa88aa','#aa99aa','#aaaaaa','#aabbaa','#aaccaa','#aaddaa','#aaeeaa','#aaffaa']
#deque to use as an offset
self.col_index = deque(range(len(self.colors)))
#eventual reference to after
self.glow = None
#add MouseOver,MouseOut events
self.bind('<Enter>',lambda e: self.__glow(true))
self.bind('<Leave>',lambda e: self.__glow(false))
def __glow(self,hover):
if hover:
#get rotation offset
ofs = self.col_index.index(0)
#apply color from rotation offset
self.configure(BACkground=self.colors[ofs])
#if the offset has not reached the end of the color list
if ofs != len(self.colors)-1:
#rotate
self.col_index.rotate(1)
#do all of this again in 50ms
self.glow = self.after(50,self.__glow,hover)
else:
#kill any expected after
self.after_cancel(self.glow)
#rewind
self.col_index.rotate(-self.col_index.index(0))
#reset to idle color
self.configure(BACkground=self.bg_idlE)
root = tk.Tk()
GlowButton(root,text='button',font='Helvetica 10 bold',bg='#aa88aa').grid()
if __name__ == '__main__':
root.mainloop()
以上是大佬教程为你收集整理的如何使 ttk 小部件在悬停时缓慢发光全部内容,希望文章能够帮你解决如何使 ttk 小部件在悬停时缓慢发光所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。