想請教各位大大,如果我想要隨著Radiobutton選擇不同而變更listbox的內容
請問該怎麼寫才好?
現在只會一值疊加上去而已... 程式碼如下
import tkinter as tk
window = tk.Tk()
window.title('Main Window')
window.geometry('600x300')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
count=0
listbox = tk.Listbox(window, selectmode=tk.MULTIPLE)
listbox.pack()
def test():
#clear(listbox)
if var.get() =='A':
for it in ('A', 'B', 'C', 'D','E'):
listbox.insert(tk.END, it)
if var.get() =='B':
for it in ('1', '2', '3', '4'):
listbox.insert(tk.END, it)
def print_selection2():
l.config(text='you have selected ' + var.get())
var = tk.StringVar()
r1 = tk.Radiobutton(window, text='A', variable=var, value='A', command = test)
r1.pack()
r2 = tk.Radiobutton(window, text='B', variable=var, value='B', command= test)
r2.pack()
b = tk.Button(window, text='execute', font=('Arial', 12), width=10, height=1, command = print_selection2)
b.pack()
window.mainloop()
Listbox.delete(pos, length),从pos开始删除多少个项目,(0, tk.END)就是全部删除;
已解決
開頭多一行listbox.delete(0, tk.END)即可
def test():
#clear(listbox)
listbox.delete(0, tk.END)