需要一个小指针。我没有看到我的错误在哪里。我试图将选项菜单的所有值都放入列表中,然后再对该列表进行操作。目前,我没有将这些值正确地放入列表中。即使我更改选项菜单的值,它也只会打印['0','0','0','0','0','0']。谢谢您的协助。
"mulitple drop down list in a for loop"""
import tkinter as tk
optionList=['0', '1', '2', '3', '4', '5', '6', '7']
drop_downs=[]
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="ivory2", bd=2,
relief=tk.RAISED)
self.parent = parent
self.pack(fill=tk.BOTH, expand=1)
self.initUI()
def initUI(self):
self.grid()
for i in range(6):
self.Var=tk.StringVar()
self.Var.set(optionList[0])
self.dropMenu=tk.OptionMenu(self, self.Var, *optionList)
self.dropMenu.config(width=7)
self.dropMenu.pack()
drop_downs.append(self.Var.get())
self.get=tk.Button(self, text="print", command=self.final)
self.get.pack()
self.pack(fill=tk.BOTH, expand=1)
def final(self):
print (drop_downs)
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("250x350")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:2)
您的问题是,您将之前设置的默认值添加到列表中。
您应该将字符串变量保存在列表中。
赞:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="ivory2", bd=2,
relief=tk.RAISED)
self.parent = parent
self.pack(fill=tk.BOTH, expand=1)
self.optionList=['0', '1', '2', '3', '4', '5', '6', '7']
self.drop_downs=[]
self.VarList = []
self.initUI()
def initUI(self):
self.grid()
for i in range(6):
self.Var=tk.StringVar()
self.Var.set(self.optionList[0])
self.dropMenu=tk.OptionMenu(self, self.Var, *self.optionList)
self.dropMenu.config(width=7)
self.dropMenu.pack()
self.VarList.append(self.Var)
#drop_downs.append(self.Var.get())
self.get=tk.Button(self, text="print", command=self.final)
self.get.pack()
self.pack(fill=tk.BOTH, expand=1)
def final(self):
for i in self.VarList:
self.drop_downs.append(i.get())
print (self.drop_downs)
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("250x350")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
if __name__ == '__main__':
main()