如何使用循环创建Tkinter小部件并为其分配自定义功能?

时间:2019-07-15 08:35:38

标签: python loops variables optimization tkinter

这是我的第一个python应用程序,我知道它的许多缺陷,因此我将尝试学习我的错误。

我想了解如何通过将tkinter小部件的声明放入循环来稍微缩短代码。

这是代码的一部分(有很多类似的部分,但我只想应用相同的逻辑):

varplies = StringVar(root)
varnotch = StringVar(root)
varspreader = StringVar(root)
varorder1 = StringVar(root)
varorder2 = StringVar(root)
def pliesto_uppercase(*args):
    varplies.set(varplies.get().upper())

def notchto_uppercase(*args):
    varnotch.set(varnotch.get().upper())

def spreaderto_uppercase(*args):
    varspreader.set(varspreader.get().upper())

def order1to_uppercase(*args):
    varorder1.set(varorder1.get().upper())

def order2to_uppercase(*args):
    varorder2.set(varorder2.get().upper())

varplies.trace_add('write', pliesto_uppercase)
varnotch.trace_add('write', notchto_uppercase)
varspreader.trace_add('write', spreaderto_uppercase)
varorder1.trace_add('write', order1to_uppercase)
varorder2.trace_add('write', order2to_uppercase)
self.rnplies_entry = Entry(self.entry_frame, background=rgbcon2((158, 174, 179)),
                           justify='center', textvariable=varplies, width=4)
self.rnnotch_entry = Entry(self.entry_frame, background=rgbcon2((158, 174, 179)),
                           justify='center', textvariable=varnotch, width=4)
self.rnspreader_entry = Entry(self.entry_frame, background=rgbcon2((158, 174, 179)),
                              justify='center', textvariable=varspreader, width=4)
self.rnorder1_entry = Entry(self.entry2_frame, background=rgbcon2((158, 174, 179)),
                            justify='center', textvariable=varorder1, width=8)
self.rnorder2_entry = Entry(self.entry2_frame, background=rgbcon2((158, 174, 179)),
                            justify='center', textvariable=varorder2, width=8)
self.rnplies_entry.grid(column=1, row=5, padx=5, sticky="SEW")
self.rnnotch_entry.grid(column=2, row=5, padx=5, sticky="SEW")
self.rnspreader_entry.grid(column=5, row=5, padx=5, sticky="SEW")
self.rnorder1_entry.grid(column=0, row=9, padx=5, pady=5, sticky="SEW")
self.rnorder2_entry.grid(column=0, row=10, padx=5, pady=5, sticky="SEW")



pfdate = StringVar(root)
pfdate.set('All')
pfmarker = StringVar(root)
pfmarker.set('All')
pfdate.trace('w', partial(changeplan, widget=pfdate))
pfmarker.trace('w', partial(changeplan, widget=pfmarker))
def planfilters():
    pdatelist = plandatefilter()
    pmarkerlist = planmarkerfilter()
    self.drop_datepl = OptionMenu(self.optionplan_frame, pfdate, *pdatelist)
    self.drop_datepl.config(bg=rgbcon2((39, 46, 46)), width=10, fg='white')
    self.drop_datepl.grid(row=5, column=0, sticky="E", padx=5, pady=5)
    self.drop_rnpl = OptionMenu(self.optionplan_frame, pfmarker, *pmarkerlist)
    self.drop_rnpl.config(bg=rgbcon2((39, 46, 46)), width=22, fg='white')
    self.drop_rnpl.grid(row=5, column=1, sticky="E", padx=5, pady=5)

简而言之,我想了解如何将其放入循环中,例如:

pl_filters = ('pfdate', 'pfmarker')
for opt_item in pl_filters:
    pl_filters[opt_item] = StringVar(root)
    pl_filters[opt_item].set('All')
    pl_filters[opt_item].trace('w', partial(changeplan, widget=pl_filters[opt_item]))

1 个答案:

答案 0 :(得分:1)

从代码的第一部分来看,您似乎在static Type GetRepoType () { // [typeName - set the typeName with args (or) config settings] switch (typeName) { case "hg": return typeof (EHgFileTag); case "git": return typeof (EGitFileTag); // Extend cases for other repositories......... default: return typeof (EHgFileTag); } } 上使用trace方法只是为了将插入的字符转换为大写。如果是这样,我建议您使用内置的此类方法来实现自己的类,这样就可以避免以后再创建它们。

此外,您可以在for循环中创建StringVarStringVar。只需将这些引用保存在列表中,以备以后使用。

Entry