【发布时间】:2021-04-16 22:17:22
【问题描述】:
我正在尝试创建一个函数,该函数会根据调用它的事件做出不同的响应。所以我有这个非常简单的用户界面,我删除了原来的标题栏并创建了我自己的。我放了一个关闭按钮和一个最小化按钮。我正在使用 and 事件来触发改变关闭按钮背景颜色的方法。我想知道我是否可以使用带有 if 语句的相同方法来更改另一个按钮的背景颜色,或者唯一的方法(甚至更好的方法?)是为它创建另一种方法。
我的代码:
from tkinter import *
from tkinter import ttk
import os
# --- main ---
root = Tk()
# --- variables ---
bg = '#2c2c2c'
width = 800
height = 500
# --- classes ---
class AvatarLoader:
def __init__(self, main):
self.main = main
root.title("Avatar Loader")
self.main.overrideredirect(True)
self.main.geometry(f'{width}x{height}+400+200')
self.main.resizable(width=True, height=True)
self.title_bar = Frame(root, bg=bg, relief = 'raised', bd=1, highlightcolor = bg,
highlightthickness=0, cursor = 'fleur')
self.title_bar.pack(fill=X)
self.title_name = Label(self.title_bar, text='Avatar Loader', bg=bg, fg='white')
self.title_name.pack(side=LEFT)
self.close_button = Button(self.title_bar, text='X', font=("Calibri",10,"bold"),
command=root.destroy,bg=bg, padx=4, pady=2,
activebackground="red", bd=0, fg='white',
activeforeground="white", highlightthickness=0,
cursor="arrow", height=1)
self.close_button.pack(side=RIGHT)
self.close_button.bind('<Enter>', self.change_on_hovering)
self.close_button.bind('<Leave>', self.return_to_normal_state)
self.minmax_button = Button(self.title_bar, text="_", font=("Calibri",10,"bold"),
command=minimize, bg=bg, padx=4, pady=2,
activebackground="#353535", bd=0, fg='white',
activeforeground="white", highlightthickness=0,
cursor="arrow", height=1)
self.minmax_button.pack(side=RIGHT)
self.minmax_button.bind('<Enter>', self.change_on_hovering)
self.minmax_button.bind('<Leave>', self.return_to_normal_state)
self.window = Canvas(root, bg=bg, highlightthickness=0, width=width, height=486)
self.window.pack(expand=1,fill=BOTH)
self.tab_open = Button(self.window, text='Open', bg="#eabb2d", fg='black', width=2,
height=1, padx=20, cursor="arrow", command=open_tab)
self.tab_open.place(anchor=W, rely=0.5)
def get_pos(event):
global xwin
global ywin
xwin = event.x
ywin = event.y
def move_window(event):
root.geometry(f"+{event.x_root - xwin}+{event.y_root - ywin}")
def change_on_hovering(self, event):
self.close_button['bg'] = 'red'
def return_to_normal_state(self, event):
self.close_button['bg'] = bg
# --- variables ---
mode_b = 1
add_open = 0
# --- functions ---
def open_tab():
pass
def minimize():
pass
av = AvatarLoader(root)
av.title_bar.bind('<B1-Motion>', AvatarLoader.move_window)
av.title_bar.bind('<Button-1>', AvatarLoader.get_pos)
av.title_name.bind('<B1-Motion>', AvatarLoader.move_window)
av.title_name.bind('<Button-1>', AvatarLoader.get_pos)
root.mainloop()
所以有问题的方法是“change_on_hovering”,我想说的是: "如果 self.minmax_button.bind('', self.change_on_hovering) 是触发器:则更改 close_button[bg],否则:更改 minmax_button[bg]
更新:
我试过这个:
self.minmax_button.bind('<Enter>', self.change_on_hovering)
self.close_button.bind('<Enter>', self.change_on_hovering)
def change_on_hovering(self, event):
if self.close_button.bind('<Enter>', self.change_on_hovering):
self.close_button['bg'] = 'red'
else:
self.minmax_button['bg'] = '#303030'
结果是在鼠标输入这些按钮中的任一按钮时,关闭按钮的背景变为“红色”
【问题讨论】:
标签: python if-statement tkinter events