【发布时间】:2016-11-22 16:08:55
【问题描述】:
我有一个列表 (elenco),我想在单击单选按钮时更改其元素,但我总是获得相同的列表
from tkinter import *
root = Tk()
el1 = [1, 2, 3, 4, 5]
el2 = [2, 2, 3, 4, 5]
elenco = []
def E(e):
elenco = e
return elenco
print (elenco)
radio1 = Radiobutton (text = 'r1', value = 1, command = lambda: E (el1)).pack()
radio2 = Radiobutton (text = 'r2', value = 2, command = lambda: E (el2)).pack()
print(elenco)
root.mainloop()
编辑:我在这里发布所有新代码。我正在重写所有程序,因为我对旧代码有一些问题。
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
class mainWindow(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
# Canvas on which buttons will be put:
mainCanvas = Canvas (self, height = 650, width = 900, scrollregion = (0, 0, 900, 6000), bg = 'white')
# Scrollbar to see all buttons (they could me also more then 60):
sbar = Scrollbar(self, command = mainCanvas.yview)
mainCanvas.config(yscrollcommand = sbar.set)
sbar.pack(side=LEFT, fill=Y)
mainCanvas.place(x = 0, y = 0)
#radiobuttons from which I'll choose the list
radio1 = Radiobutton(self, text = 'lis1', value = 1).place (x = 920, y = 150)
radio2 = Radiobutton(self, text = 'lis2', value = 2).place (x = 920, y = 170)
radio3 = Radiobutton(self, text = 'lis3', value = 3).place (x = 920, y = 190)
if __name__ == '__main__':
root = Tk()
root.title("main")
root.geometry("1130x650+100+10")
app = mainWindow(root)
root.mainloop()
在这段代码中没有按钮,但是如果你运行程序你会看到一个白色的画布,它们会在那里。还将有一个 EntryBox,您可以在其中编写元素 (-s) 名称(或其中的一部分),您将获得与该名称对应的按钮。但是,如果您想按列表切换到另一个列表?那么,如果我想要第二个列表中而不是第一个列表中的 element4 的按钮?看起来没什么用,但实际上主列表中将有 720 个元素,所有其他列表都将是该列表的一部分。列表对应于区域,主列表是所有区域的列表。那么,如果我想要一些以字母“a”开头的元素,但我想要它来自特定列表(区域)而不是来自任何地方?
如果您有更好的想法,请通过单选按钮选择列表,我会使用它。我的问题是按列表移动到另一个列表
【问题讨论】:
-
你的程序很乱。函数和回调命令都是废话。我用有点不同的方式写了它。期待您的反馈。
-
我真的不明白你想说什么。 “如果你不习惯用英语写作,请朋友帮你校对。” -stackoverflow.com/help/how-to-ask
标签: list tkinter radio-button python-3.3