簡體   English   中英

刪除 ttk 組合框鼠標滾輪綁定

[英]Remove ttk Combobox Mousewheel Binding

我有一個 ttk 組合框,我想從鼠標滾輪上解除綁定,以便在組合框處於活動狀態時使用滾輪滾動不會更改值(而是滾動框架)。

我試過解除綁定以及綁定到空函數,但都不起作用。 見下文:

import Tkinter as tk
import ttk


class app(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.interior = tk.Frame(self)

        tkvar = tk.IntVar()
        combo = ttk.Combobox(self.interior,
                             textvariable = tkvar,
                             width = 10,
                             values =[1, 2, 3])
        combo.unbind("<MouseWheel>")
        combo.bind("<MouseWheel>", self.empty_scroll_command)
        combo.pack()
        self.interior.pack()


    def empty_scroll_command(self, event):
        return

sample = app()
sample.mainloop()

任何幫助將不勝感激。

謝謝!

默認綁定在內部小部件類上,它在您添加任何自定義綁定后執行。 您可以刪除會影響整個應用程序的默認綁定,或者您可以將特定小部件綁定到返回字符串"break"的自定義函數,這將阻止運行默認綁定。

移除類綁定

ttk 組合框的內部類是TCombobox 您可以將其傳遞給unbind_class

# Windows & OSX
combo.unbind_class("TCombobox", "<MouseWheel>")

# Linux and other *nix systems:
combo.unbind_class("TCombobox", "<ButtonPress-4>")
combo.unbind_class("TCombobox", "<ButtonPress-5>")

添加自定義綁定

當對個人的綁定返回字符串"break" ,這將阻止處理默認綁定。

# Windows and OSX
combo.bind("<MouseWheel>", self.empty_scroll_command)

# Linux and other *nix systems
combo.bind("<ButtonPress-4>", self.empty_scroll_command)
combo.bind("<ButtonPress-5>", self.empty_scroll_command)

def empty_scroll_command(self, event):
    return "break"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM