【发布时间】:2019-03-21 07:25:41
【问题描述】:
需要一个小指针。我没有看到我的错误在哪里。我正在尝试将选项菜单的所有值放入列表中,然后对该列表执行某些操作。现在,我没有正确地将值放入列表中。即使我更改选项菜单的值,它也只会打印 ['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()
【问题讨论】:
-
self.Var.set(optionList[0]) 中的默认值设置为 0,然后在选择任何选项之前将其附加到 drop_downs.append(self.Var.get())。
标签: python-3.x list tkinter drop-down-menu append