【问题标题】:Using if statements with tkinter Events将 if 语句与 tkinter 事件一起使用
【发布时间】: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


    【解决方案1】:

    event.widget是触发事件的widget,所以在两个函数里面使用event.widget["bg"] = ...

    def change_on_hovering(self, event):
        event.widget['bg'] = 'red'
    
    def return_to_normal_state(self, event):
        event.widget['bg'] = bg
    

    【讨论】:

    • 我不确定这应该如何发生。可以举个例子吗?
    • @AleksK。使用示例代码更新答案。
    • 无法让它工作。在您的示例中,没有“if”语句。我通过添加我尝试过的内容来更新问题,但它没有用。也许它可以作为我正在尝试做的事情的一个例子。
    • if 没有必要使用我提出的解决方案。我已经测试过,它对我有用。
    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多