亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按鈕的 Tkinters 突出顯示對我不起作用

按鈕的 Tkinters 突出顯示對我不起作用

紅顏莎娜 2022-06-22 16:29:26
根據這篇文章中接受的答案,按鈕上的使用.configure(highlightbackground='red')應該在按鈕周圍應用顏色,但是在測試中我無法重現海報在他們的 gif 錄制中展示的內容。這是我的測試用例:(注意即使復制粘貼海報代碼我也無法獲得它們顯示的突出顯示效果)import tkinter as tkroot = tk.Tk()btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4, activebackground="#ffffff",                activeforeground="#000000", highlightbackground='red', highlightcolor='red')btn.pack()btn.focus_set()root.mainloop()結果應用程序:在此處輸入圖像描述通過一些廣泛的搜索,我在highlightbackground關于同一問題的 Q/A 方式中沒有找到太多信息,因此可能缺少某些內容。我還嘗試設置焦點,因為本文檔指出小部件需要焦點,但結果相同。也許它可能與版本或操作系統相關......操作系統 - Windows 10 專業版蟒蛇 - 3.6.2使用 Krrr 的帖子更新了示例。所以這確實有點工作,但是這里的問題是它正在調整按鈕的大小并且沒有提供正確的突出顯示顏色。import tkinter as tkdef ResponsiveWidget(widget, *args, **kwargs):    bindings = {        '<FocusIn>': {'highlightbackground': 'red', 'highlightcolor':'red'},        '<FocusOut>': {'highlightbackground': '#d9d9d9', 'highlightcolor':'SystemButtonFace'},        '<Enter>': {'state': 'active'},        '<Leave>': {'state': 'normal'}    }    for k, v in bindings.items():        root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))def update_active(event):    global previous_button    if previous_button != event.widget:        previous_button.config(default='normal')        event.widget.config(default='active')        previous_button = event.widgetroot = tk.Tk()button_list = []previous_button = Nonefor i in range(5):    if i == 0:        button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,                                     activebackground="#ffffff", activeforeground="#000000", default='active'))        previous_button = button_list[-1]    else:        button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,                                     activebackground="#ffffff", activeforeground="#000000", default='normal'))    button_list[-1].pack(padx=5, pady=5)    button_list[-1].bind('<ButtonRelease-1>', update_active)root.mainloop()
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

不幸的是,Windows 操作系統似乎沒有正確觸發state和default小部件配置。但是,這可以通過您自己的綁定來實現。


如果您只有少數需要此行為的小部件,則可以創建一個小部件包裝器:


def ResponsiveWidget(widget, *args, **kwargs):

    bindings = {

        '<FocusIn>': {'default':'active'},    # for Keyboard focus

        '<FocusOut>': {'default': 'normal'},  

        '<Enter>': {'state': 'active'},       # for Mouse focus

        '<Leave>': {'state': 'normal'}

    }

    # Create the widget instance

    w = widget(*args, **kwargs)


    # Set the bindings for the widget instance

    for k, v in bindings.items():

        w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))


    # Remember to return the created and binded widget

    return w


btn = ResponsiveWidget(tk.Button, root, text='test3', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='red', highlightcolor='green')


btn2 = ResponsiveWidget(tk.Button, root, text='test4', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='green', highlightcolor='red')

另一方面,如果您希望小部件的整個類始終正確觸發默認/狀態,則可以bind_class改用:


bindings = {

    '<FocusIn>': {'default':'active'},    # for Keyboard focus

    '<FocusOut>': {'default': 'normal'},  

    '<Enter>': {'state': 'active'},       # for Mouse focus

    '<Leave>': {'state': 'normal'}

}

for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))

這似乎很好地觸發了事件。


如果您只想復制突出顯示顏色的功能,則不太理想的方法是更改highlightcolor焦點配置:


bindings = {

        '<FocusIn>': {'highlightcolor':'red'},

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},

        '<Enter>': {'state': 'active'},

        '<Leave>': {'state': 'normal'}

    }

for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))


# Note this method requires you to set the default='active' for your buttons


btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

                activeforeground="#000000", highlightcolor='SystemButtonFace', default='active')


# ...

我認為這更像是一種 hacky 方法。


編輯:為了完整起見,這里有一個 MCVE 使用bind_class:


import tkinter as tk


root = tk.Tk()

bindings = {

        '<FocusIn>': {'highlightcolor':'red'},

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},

        '<Enter>': {'state': 'active'},

        '<Leave>': {'state': 'normal'}

    } 


for k, v in bindings.items():

    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))


btns = list(range(5))

for btn in btns:

    btns[btn] = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5, activebackground="#ffffff",

        activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)

    btns[btn].pack()


btns[0].focus_set()

root.mainloop()

和 MCVE 使用ResponsiveWidget功能:


import tkinter as tk


root = tk.Tk()

def ResponsiveWidget(widget, *args, **kwargs):

    bindings = {

        '<FocusIn>': {'highlightcolor':'red'},    # for Keyboard focus

        '<FocusOut>': {'highlightcolor': 'SystemButtonFace'},  

        '<Enter>': {'state': 'active'},       # for Mouse focus

        '<Leave>': {'state': 'normal'}

    }

    # Create the widget instance

    w = widget(*args, **kwargs)


    # Set the bindings for the widget instance

    for k, v in bindings.items():

        w.bind(k, lambda e, kwarg=v: e.widget.config(**kwarg))


    # Remember to return the created and binded widget

    return w


btns = list(range(5))

for btn in btns:

    btns[btn] = ResponsiveWidget(tk.Button, root, text=f'test{btn}', bg="#000000", fg="#ffffff", highlightthickness=10, activebackground="#ffffff",

        activeforeground="#000000", highlightcolor='SystemButtonFace', default='active', padx=5, pady=5)

    btns[btn].pack()


btns[0].focus_set()

root.mainloop()


查看完整回答
反對 回復 2022-06-22
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

謝謝您的問題,您的代碼很好,只需使用即可解決您的問題default="active"


import tkinter as tk



root = tk.Tk()


btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4,  

activebackground="#ffffff",

                activeforeground="#000000", highlightbackground='red', 

default="active", highlightcolor='red')

btn.pack()

btn.focus_set()

root.mainloop()


查看完整回答
反對 回復 2022-06-22
  • 2 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號