gpt4 book ai didi

python - 调用命令后 Tkinter 按钮突出显示功能停止工作

转载 作者:行者123 更新时间:2023-12-01 00:25:06 25 4
gpt4 key购买 nike

我一直在 tkinter 中开发我的第一个 GUI - 我使用的是 Windows。我现在的目标是拥有可以实现这些目标的按钮:

  1. 当鼠标悬停在按钮上时,按钮会突出显示。
  2. 如果单击该按钮,该按钮将保持突出显示状态。
  3. 一次只能“选择”(点击突出显示)一个按钮。

我最初以为我已经完成了这个!但我现在意识到我的工作还没有完成。

这是我所看到的:

  1. 我将鼠标悬停在按钮 A 上。它会突出显示! (好)
  2. 我点击按钮 A。它保持突出显示! (好)
  3. 我将鼠标悬停在按钮 B 上。它会突出显示! (好)
  4. 我点击按钮 B。它保持突出显示! A 的高亮部分被删除! (好)
  5. 我将鼠标悬停在按钮 A 上。它没有突出显示。 (不好)

当我单击按钮 B 时,我正在调用按钮 A 上的 default_coloring 类函数。但是,这似乎关闭了按钮 A 的突出显示功能,并且根据我在顶部。

即使在调用命令之后,如何确保按钮继续正常工作?我是否以错误的方式处理这个问题?

  import tkinter as tk

blue = '#0000BB'
white = '#FFFFFF'

class HoverButton(tk.Button):
def __init__(self, master, position = None, **kw):
tk.Button.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.defaultForeground = self["foreground"]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
self.bind("<Button-1>", self.hover_click)
self.state = 0
self.position = position

def on_enter(self, e):
if self.state == 0:
self['background'] = self['activebackground']
self['foreground'] = self['activeforeground']

def on_leave(self, e):
if self.state == 2:
self.state = 0
if self.state == 0:
self['background'] = self.defaultBackground
self['foreground'] = self.defaultForeground

def hover_click(self, e):
self.state += 1
self.state = self.state % 3
if self.state == 2:
self['background'] = self.defaultBackground
self['foreground'] = self.defaultForeground

def default_coloring(self):
self['background'] = self.defaultBackground
self['foreground'] = self.defaultForeground

class AddOnFrame(tk.Frame):
def __init__(self, master):
self.selectedbutton = None
super().__init__(master)
games = ['A','B','C']
self.objs = list()
self['bg'] = blue
for i in range(3):
self.objs.append(HoverButton(self,position = i, text = games[i].upper(), activebackground = white,activeforeground = blue,fg = white, bg = blue, borderwidth=0, relief = 'flat', highlightbackground = white))
self.objs[i]['command'] = lambda c=i: self._hover_button_clicked(self.objs[c])
self.objs[i].grid(row = i, column = 0, sticky = tk.W + tk.E)
self.blanklabel = tk.Label(self, text = '', background = white)
self.blanklabel.grid(row = 0, column = 1,rowspan = 10, sticky = tk.N + tk.E + tk.W + tk.S)
self.grid_columnconfigure(1, weight=1, minsize=10)
self.grid_columnconfigure(2, weight=1, minsize=500)
self.grid_columnconfigure(3, weight=1, minsize=500)
self.grid_columnconfigure(4, weight=1, minsize=500)
self.pack(expand = True)

def _hover_button_clicked(self, HoverButton):
self.lastbutton = self.selectedbutton
if self.lastbutton != None:
self.objs[self.lastbutton].default_coloring()
self.selectedbutton = HoverButton.position

window = tk.Tk()
window.geometry('1750x950')
window['bg'] = blue
window.title('Testing')
lf = AddOnFrame(window)
lf['bg'] = blue

window.mainloop()

最佳答案

我想我找到了问题的主要根源。单击另一个按钮时,您将恢复上次单击的按钮的颜色,但不会重置其状态。将 default_coloring 函数更改为:

def default_coloring(self):
self.state = 0
self['background'] = self.defaultBackground
self['foreground'] = self.defaultForeground

但是如果再次按下相同的按钮,您还应该防止default_coloring:

def _hover_button_clicked(self, HoverButton):
self.lastbutton = self.selectedbutton
if (self.lastbutton != None) and (self.lastbutton != HoverButton.position):
self.objs[self.lastbutton].default_coloring()
self.selectedbutton = HoverButton.position

关于python - 调用命令后 Tkinter 按钮突出显示功能停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58646740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com