我正在考慮在一個事件后禁用滑鼠雙擊事件。如果我雙擊串列框中的專案,則事件會禁用,直到我按下“啟用雙擊”按鈕。如何存檔?
from tkinter import *
def go(event):
cs = Lb.curselection()
# Updating label text to selected option
w.config(text=Lb.get(cs))
# Setting Background Colour
for list in cs:
if list == 0:
top.configure(background='red')
elif list == 1:
top.configure(background='green')
elif list == 2:
top.configure(background='yellow')
elif list == 3:
top.configure(background='white')
top = Tk()
top.geometry('250x275')
top.title('Double Click')
# Creating Listbox
Lb = Listbox(top, height=6)
# Inserting items in Listbox
Lb.insert(0, 'Red')
Lb.insert(1, 'Green')
Lb.insert(2, 'Yellow')
Lb.insert(3, 'White')
# Binding double click with left mouse
# button with go function
Lb.bind('<Double-1>', go)
Lb.pack()
# Creating Edit box to show selected option
w = Label(top, text='Default')
w.pack()
# Creating Enable button to enable double clicking
enable_btn = Button(top, text = 'Enable Double Click')
enable_btn.pack(pady = 10)
top.mainloop()
uj5u.com熱心網友回復:
要禁用雙擊,請系結到該事件,然后讓您的函式回傳 string "break"。您可以使用變數來觸發該行為。
首先定義一個標志,然后是一個可以設定標志的函式:
double_click_enabled = False
def enable_double_clicking(enable):
global double_click_enabled
double_click_enabled = enabled
接下來,定義你的按鈕來呼叫這個函式。在這個例子中,我將展示兩個用于啟用和禁用的按鈕:
enable_btn = Button(top, text = 'Enable Double Click', command=lambda: enable_double_clicking(True))
disable_btn = Button(top, text = 'Disable Double Click', command=lambda: enable_double_clicking(False))
最后,檢查go函式頂部的標志。如果禁用了雙擊,則回傳字串“break”,這會阻止函式的結果執行并禁用任何默認行為。
def go(event):
if not double_click_enabled:
return "break"
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375684.html
