from tkinter import *
root = Tk()
root.geometry("500x300")
def update(data)。
# Clear the listbox: 清除串列框。
my_list.delete(0, END)
# 添加拓撲到串列框中
for item in data:
my_list.insert(END, item)
def fillout(e)。 # $$%$%$%$.
my_entry.delete(0, END)
my_entry.insert(0, my_list.get(ANCHOR))
my_list.selection_clear(0, END)
my_entry .focus_set()
my_entry .icursor(END)
def check(e)。
typed = my_entry.get()
if typed == ''/span>:
data = toppings
else:
data = []
for item in toppings:
if typed.lower() in item.lower() 。
data.append(item)
update(data)
my_label = Label(root, text="Start", fg="灰色")
my_label.pack(pady=20)
my_entry = Entry(root)
my_entry.pack()
my_list = Listbox(root, width=50)
my_list.pack(pady=40)
toppings = ["Pepperoni", "Peppers", "Mushrooms", "Cheese", "Onions"]
my_list.bind("<<ListboxSelect>>"/span>, fillout)
my_entry.bind("<KeyRelease>", check)
root.mainloop()
我正試圖移除焦點并取消對條目的小部件(串列框)的選擇。簡而言之,我想要的行為是,當從串列框中選擇一個選項時,移除各種焦點,并立即將插入游標放在輸入中。我試著用(.focus_set()和selection_clear()),但似乎對我不起作用。我請求您的幫助和指導。
uj5u.com熱心網友回復:
你需要等待設定焦點,直到所有其他待處理的事件都被處理之后。你可以通過使用after_idle來做到這一點,就像下面的例子:
my_entry.after_idle(my_entry.focus_set)
uj5u.com熱心網友回復:
它看起來像"<<ListboxSelect>>"事件將在你的事件處理程式之后進行選擇。
我沒有找到一個好的方法,所以這里有一個愚蠢的方法。
from tkinter import *
root = Tk()
root.geometry("500x300")
def select(e)。
selection = my_list.get(ANCHOR)
my_list.selection_clear(0, END)
root.after(100, fillout, selection)
def fillout(selection)。
my_entry.focus_set()
my_entry.delete(0, END)
my_entry.insert(0, selection)
my_entry.icursor(END)
def check(e)。
typed = my_entry.get()
if typed == ''/span>:
data = toppings
else:
data = []
for item in toppings:
if typed.lower() in item.lower() 。
data.append(item)
update(data)
my_label = Label(root, text="Start", fg="灰色")
my_label.pack(pady=20)
my_entry = Entry(root)
my_entry.pack()
selection = ''。
toppings = ("Pepperoni", "Peppers", "Mushrooms", "cheese", "Onions")
my_list = Listbox(root, width=50)
my_list.pack(pady=40)
my_list.insert(0, *toppings)
my_list.bind("<<ListboxSelect>>"/span>, select)
my_entry.bind("<KeyRelease>", check)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319458.html
標籤:
