使用Tkinter在未知长度的可迭代中为每个项创建一个条目

时间:2014-04-20 07:50:45

标签: python dictionary tkinter


在邮寄结束时看到更新!

好的,让我看看能否正确解释一下。我正在使用Tkinter编写脚本,本质上将为更大的代码包设置配置文件。其中一个Toplevel弹出窗口将用于编辑已存在的记录。我要做的是创建一个Toplevel小部件,它将包含几个静态项加上字典中的键/值对,我想知道是否可以创建一个小部件,其中输入框的数量取决于长度字典。

例如,如果我有一个包含此字典的字典:

accounts={'abcd@gmail.com':{'pass':'password','type':'Gmail','disabled':False}}

我想要一个用于电子邮件,密码,类型和禁用设置的Entry Widget,并使用这个:

accounts={'abcd@gmail.com':{'pass':'password','type':'Gmail','disabled':False, 'retries':'3'}}

与上面的相同,加上我可以通过点击按钮检索的重试选项,即:。

b=Button(top, text='Save Changes', command=getvariables)

----------------------------- UPDATE ----------------- --------------

@atlassologist你的解决方案让我走上正轨。我修改它,直到它给我我正在寻找的输出

here是github上的完整configui.py代码

以下是最终有效的代码的缩写:

from Tkinter import *

accountsdict={'abcde@gmail.com':{'disabled':'True', 'type':'live','password':'pword'}}

class EditAccounts:
    def __init__(self, parent, account):
        self.value_list=[]
        top=self.top=Toplevel(parent)
        Label(top, text="Editing Account information").grid(column=0, row=0, columnspan=2, sticky='ew')
        row=1
        for k,v in accountsdict[account].iteritems():
            var=StringVar()
            var.set(v)
            Label(self.top, text=k).grid(row=row, column=0)
            Entry(self.top, textvariable=var).grid(row=row, column=1)
            row+=1
            self.value_list.append((k,var))
        Button(self.top, text="Cancel", command=lambda: self.top.destroy()).grid(column=1, row=20)
        Button(self.top, text="Save", command=self.save_changes).grid(column=0, row=20)

    def save_changes(self):
        for i in self.value_list:
            print i[0], i[1].get()

class SelectAccountToEdit:
    def __init__(self):
        root=self.root=Tk()
        Label(root, text="Select an account to edit").grid(column=0, row=0, columnspan=2)
        self.accounts=Listbox(root)
        for k in accountsdict.keys():
            self.accounts.insert(END, k)
        self.accounts.grid(column=0, row=1, rowspan=3, columnspan=2)
        self.ok=Button(root, text="Select", command=self.select)
        self.done=Button(root, text="Done", command=self.root.destroy)
        self.ok.grid(column=0,row=4,sticky='ew')
        self.done.grid(column=1,row=4,sticky='ew')
        self.accounts.grid(column=0,row=1,columnspan=2,rowspan=3, sticky='ew')
        mainloop()

    def select(self):
        acnt=self.accounts.get(self.accounts.curselection())
        d=EditAccounts(self.root, acnt)
        self.root.wait_window(d.top)

SelectAccountToEdit()

1 个答案:

答案 0 :(得分:1)

这是您想要的一些功能的简化示例。希望它能让你在右脚出发。我还建议查看iterating over dictionariespickle模块的文档,这对于保存和访问字典非常有帮助。

def save_changes():
    for i in value_list: # access the list of stringvars
        print i.get()    # and print the .get() method of each

root = Tk()

d = {'item1':'value1', 'item2':'value2'}
value_list = []

row=1
for k,v in d.iteritems(): # iterate over k,v in dict
    var = StringVar()
    var.set(v) # set var to value of dict item
    Label(root, text=k).grid(row=row, column=0)
    Entry(root, textvariable=var).grid(row=row, column=1)
    row+=1
    value_list.append(var) # append the stringvar instance to a list

Button(root, text='Save', command=save_changes).grid()

mainloop()

注意:还有其他方法可以做到这一点,甚至可能是更好的方法,并且您很可能需要对其进行大量调整才能使用您的数据和规范。这只是一个起点。