【问题标题】:why is my list not adding my option menu values?为什么我的列表没有添加我的选项菜单值?
【发布时间】: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


【解决方案1】:

您的问题是您将之前设置的默认值附加到列表中。

你应该把你的字符串变量保存在一个列表中。

像这样:

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()

【讨论】:

  • 感谢您的帮助。我怀疑问题是在错误的阶段添加了元素,但我不知道如何编码。您的解决方案有效,现在我能够处理我的程序的下一部分。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 2019-05-20
相关资源
最近更新 更多