[英]Adding items from one tkinter listbox to another
我有两个tkinter列表框(父和子)和两个按钮(add_button和remove_button)。 子列表框通过add_button从父列表框获取项目,还有一个选项可以通过remove_button从子列表框删除项目。
我的问题是,在GUI加载并按下add_button之后,父列表框中的第一项即使未处于活动状态也被添加到子列表框中。 child_frame也会发生同样的情况,即使按下了remove_button,即使它不是ACTIVE,也将其删除。 我想要一种防止这种情况的方法。 另外,无论将add_button按下多少次,我都希望仅将一项添加到子级列表框中。
下面的代码是另一个更大的代码的一部分。 解决上述问题将为您提供任何帮助。 提前致谢。
这是代码:
from Tkinter import *
root = Tk()
root.geometry('330x200')
names = ['Bill', 'Jack', 'Joanne', 'Ann', 'Dave', 'Jane']
def add_name():
x = parent.get(ACTIVE)
child.insert(END, x)
def remove_name():
child.delete(ACTIVE)
parent = Listbox(root)
for name in names:
parent.insert(END, name)
parent.place(x=5, y=5)
add_button = Button(root, text='Add',
command=add_name)
add_button.place(x=148, y=5)
remove_button = Button(root, text='Remove',
command=remove_name)
remove_button.place(x=138, y=50)
child = Listbox(root)
child.place(x=200, y=5)
root.mainloop()
为什么不更换ACTIVE
通过curselection
-
def add_name():
if len(parent.curselection()) != 0:
x = parent.get(parent.curselection())
child.insert(END, x)
else:
print "select an element first"
def remove_name():
if len(parent.curselection()) != 0:
child.delete(child.curselection())
else:
print "select an element first"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.