[英]How to 'fix' selected item in tkinter.Listbox?
這是我的代碼的摘錄:
import tkinter as tk
def evaluate(event):
print(list_box.curselection())
root = tk.Tk()
var = tk.StringVar()
var.set(0)
entry = tk.Entry(root, textvariable = var)
entry.place(x = 150, y = 0, width = 20)
entry.bind("<Return>", evaluate)
list_box = tk.Listbox(root, selectmode = 'single')
list_box.place(x = 0, y = 0)
lst = [1, 2, 3]
for elem in lst:
list_box.insert('end', elem)
list_box.selection_set(first = 0)
list_box.bind("<<ListboxSelect>>", evaluate)
root.mainloop()
問題是我想以某種方式“修復”tkinter.Listbox 中最后選擇的值。 我的意思是,如果我們在 window 中輸入一些內容到條目中,那么在某些情況下(可能,這取決於您如何單擊輸入框)列表中選擇的值將會丟失。 例如,是否可以保存最后選擇的值?
我是 Python 的新手,尤其是 tkinter package 的新手,因此我們將不勝感激。
問題是,一旦您 select 其他類似條目的輸入,您的列表框就會丟失顯示的選擇。 我刪除了雙擊的默認綁定,但是如果您仍想使用它來編寫輸入,我無法刪除單擊(B1-Motion dosent 也可以)。 至少我不知道怎么做。 我建議將條目用作顯示,反之亦然。
import tkinter as tk
def evaluate(event):
print(list_box.curselection())
root = tk.Tk()
var = tk.StringVar()
var.set(0)
entry = tk.Entry(root, textvariable = var)
entry.place(x = 150, y = 0, width = 20)
entry.bind("<Return>", evaluate)
entry.bind('<Double-Button-1>', lambda e: "break")
list_box = tk.Listbox(root, selectmode = 'single')
list_box.place(x = 0, y = 0)
lst = [1, 2, 3]
for elem in lst:
list_box.insert('end', elem)
list_box.selection_set(first = 0)
list_box.bind("<<ListboxSelect>>", evaluate)
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.