按钮悬停效果有效,而菜单按钮悬停效果不起作用

问题描述 投票:0回答:1

进口 从 tkinter 导入 * 从 PIL 导入 ImageTk、Image、ImageDraw

#1f1f1f #181818 #262626

def create_button(parent, text, command=None, side=RIGHT): 按钮=按钮(父,文本=文本,字体=(“Segoe”,10),bd=0,bg=“#181818”,fg=“白色”,command=命令,activebackground='#3e4042',activeforeground= “白色的”) 按钮.pack(侧面=侧面) 返回按钮

按钮悬停效果 def悬停效果(按钮,输入颜色,离开颜色): def on_enter(事件): 按钮['bg'] = Enter_color

def on_leave(事件): 按钮['bg'] = left_color

返回on_enter,on_leave

def on_drag(事件): xwin = window.winfo_x() - event.x_root ywin = window.winfo_y() - event.y_root window.geometry(f'+{event.x_root + xwin}+{event.y_root + ywin}')

def round_rectangle(画布, x1, y1, x2, y2, r=25, **kwargs):
点 = (x1+r, y1, x1+r, y1, x2-r, y1, x2-r, y1, x2, y1, x2, y1+r, x2, y1+r, x2, y2-r, x2 , y2-r, x2, y2, x2-r, y2, x2-r, y2, x1+r, y2, x1+r, y2, x1, y2, x1, y2-r, x1, y2-r, x1 , y1+r, x1, y1+r, x1, y1) 返回canvas.create_polygon(points, **kwargs, smooth=True)

#占位符命令 def dummy_command(): print("这是一个临时占位符")

def file_menu(): round_rectangle(侧边栏, 55, 0, 193, 290, fill="red")

窗口设置 窗口 = Tk() window.configure(bg="#1f1f1f") window.title("编辑器") window.overrideredirect(True)

标题框 title_bar = 画布(窗口,bg="#181818",highlightthickness=0) icon = Image.open('标志设计.png') 调整大小 = icon.resize((16, 16),) 图片 = ImageTk.PhotoImage(调整大小) 标签 = 标签(标题栏, 图像=图片, bg="#181818") label.pack(side=LEFT, padx=18)

title_bar.pack(锚=N,填充=X) 侧边栏=画布(窗口,宽度= 600,高度= 300,highlightbackground =“#262626”,bg =“#1f1f1f”) sidebar.pack(side=LEFT, fill=Y)

按钮 exit_button = create_button(title_bar, ' ✕ ', window.quit) Expand_button = create_button(title_bar, ' ☐ ') minimize_button = create_button(title_bar, ' ― ', lambda: window.iconify() if not window.wm_state() == 'iconic' else window.deiconify())

def create_menu_button(parent, text, options, font=("Segoe", 10)): menu_button = Menubutton(父级,text=text,bg="#181818",fg="white",indicator=0,font=font) menu_button.menu = 菜单(menu_button,撕下=0) 菜单按钮[“菜单”] = 菜单按钮.菜单

vars_list = [IntVar() for _ in range(len(options))]

对于选项中的标签、变量、命令: menu_button.menu.add_checkbutton(标签=标签,变量=var,命令=命令,字体=字体)

menu_button.pack(side='left') 返回菜单_按钮,变量_列表

文件菜单 file_options = [("打开", IntVar(), dummy_command), (“保存”,IntVar(),dummy_command), (“退出”, IntVar(), window.destroy)]

file_button,file_vars = create_menu_button(标题栏,“文件”,file_options)

编辑菜单 edit_options = [("复制", IntVar(), dummy_command), (“粘贴”,IntVar(),dummy_command)]

edit_button,edit_vars = create_menu_button(title_bar,“编辑”,edit_options)

terminal_button = create_button(title_bar, ' 终端 ', side=LEFT) run_button = create_button(title_bar, ' 运行 ', side=LEFT)

拖动窗口 def get_pos(事件): xwin = window.winfo_x() ywin = window.winfo_y() startx = 事件.x_root 启动=事件.y_root xwin = xwin - 开始x ywin = ywin - 开始

def move_window(事件): window.geometry('+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin)) startx = 事件.x_root 启动 = event.y_root

title_bar.bind('', move_window)

title_bar.bind('', get_pos)

按钮悬停效果 exit_button.bind('', *hover_effect(exit_button, '红色', '#181818')) exit_button.bind('', *hover_effect(exit_button, '#181818', '#181818')) Expand_button.bind('', *hover_effect(expand_button, '#3e4042', '#181818')) Expand_button.bind('', *hover_effect(expand_button, '#181818', '#181818')) minimize_button.bind('', *hover_effect(minimize_button, '#3e4042', '#181818')) minimize_button.bind('', *hover_effect(minimize_button, '#181818', '#181818')) file_button.bind('', *hover_effect(file_button, '#3e4042', '#181818')) file_button.bind('', *hover_effect(file_button, '#181818', '#181818')) edit_button.bind('', *hover_effect(edit_button, '#3e4042', '#181818')) edit_button.bind('', *hover_effect(edit_button, '#181818', '#181818')) Terminal_button.bind('', *hover_effect(terminal_button, '#3e4042', '#181818')) Terminal_button.bind('', *hover_effect(terminal_button, '#181818', '#181818')) run_button.bind('', *hover_effect(run_button, '#3e4042', '#181818')) run_button.bind('', *hover_effect(run_button, '#181818', '#181818'))

主循环 window.mainloop()

当我将鼠标悬停在按钮上时,按钮具有 #3e4042 颜色,并且我尝试对菜单按钮使用相同的功能,但它们仍然显示为白色:(

我尝试使用 ChatGPT,但不幸的是无法解决问题。

python user-interface tkinter widget
1个回答
0
投票

尝试:

activebackground='#3e4042', activeforeground="white"

10号线

© www.soinside.com 2019 - 2024. All rights reserved.