这两个函数在Tkinter中的区别是什么?

时间:2014-02-25 19:49:18

标签: python-2.7 tkinter

为什么这两个功能不同?

    def other_entry1(self, selection, row, el, var):
        if selection == "Other":
            var = StringVar()
            el = Entry(self.frame1, textvariable=var)
            el.grid(row=row, column=6)
    #Calling it as part of an optionMenu
    self.e33 = OptionMenu(self.frame1, self.ea_tf, *fixtures, command= lambda selection:self.other_entry1(selection,15, self.e33, self.ea_tf))

另一个:

    def other_entry2(self, selection):
        if selection == "Other":
            self.ea_tf = StringVar()
            self.e33 = Entry(self.frame1, textvariable=self.ea_tf)
            self.e33.grid(row=15, column=6)

    #Calling it in an optionMenu
    self.e33 = OptionMenu(self.frame1, self.ea_tf, *fixtures, command=self.other_entry2)

我希望能够多次调用第一个函数,然后告诉它创建哪个输入框而不是创建几个单独的函数。

编辑:第二个函数是不是只是跳过了在参数中替换的步骤?

1 个答案:

答案 0 :(得分:0)

self.s33 引用 OptionMenu

第二个功能通过引用self.e33覆盖Entry,您无法使用self.e33来访问OptionMenu

self.s33el的第一个功能复制引用然后通过引用el覆盖Entry,但您仍然可以使用self.s33来访问OptionMenu 1}}

在PythonTutor.com上使用“Visual Execution”查看简单示例:

使用链接打开页面示例,单击“Visual Execution”然后您可以使用“Forward”按钮逐步查看示例如何工作


您可以使用第一个功能创建多个Entry,但不需要发送self.e33self.ea_tf,因为函数不会使用它。

您得到与

相同的结果
def other_entry1(self, selection, row):
    if selection == "Other":
        var = StringVar()
        Entry(self.frame1, textvariable=var).grid(row=row, column=6)

#Calling it as part of an optionMenu
self.e33 = OptionMenu(self.frame1, self.ea_tf, *fixtures, command=lambda selection:self.other_entry1(selection,15))

问题是访问EntryStringVar()以获取Entry的价值

self.all_vars = {} # dictionary

def other_entry1(self, selection, row):
    if selection == "Other":
        self.all_vars[row] = StringVar()
        Entry(self.frame1, textvariable=self.all_vars[row]).grid(row=row, column=6)

#Calling it as part of an optionMenu
self.e33 = OptionMenu(self.frame1, self.ea_tf, *fixtures, command=lambda selection:self.other_entry1(selection,15))

# another place

row = 15
print "row:", row
print "value in Entry:", self.all_vars[row].get()

编辑:工作示例

#!/usr/bin/env python

from Tkinter import *

class App():

    def __init__(self, master):
        self.master = master

        self.all_entries = {} # empty dictionary
        self.all_entries_vars = {} # empty dictionary

        self.all_optionmenus = {} # empty dictionary
        self.all_optionmenus_vars = {} # empty dictionary

        fixtures = ["One", "Two", "Tree", "Other"]

        # create 5 options menu
        for i in range(5):
            self.all_optionmenus_vars[i] = StringVar()
            self.all_optionmenus_vars[i].set("One")

            self.all_optionmenus[i] = OptionMenu(self.master, self.all_optionmenus_vars[i], *fixtures, command=lambda selection, col=i:self.addEntry(selection, col))
            self.all_optionmenus[i].grid(row=1, column=i)

        Button(master, text="Print OptionMenus Vars", command=self.printOptionMenus).grid(row=2, column=0, columnspan=5)
        Button(master, text="Print Entries Vars", command=self.printEntries).grid(row=3, column=0, columnspan=5)

    def run(self):
        self.master.mainloop()

    def addEntry(self, selection, col):

        if selection == "Other":

            # if Entry was created before
            if col in self.all_entries:
                # show existing Entry
                self.all_entries[col].grid(row=0, column=col)
            else:
                # create new Entry
                self.all_entries_vars[col] = StringVar()
                self.all_entries[col] = Entry(self.master, textvariable=self.all_entries_vars[col])
                self.all_entries[col].grid(row=0, column=col)

        # if Entry was created before
        elif col in self.all_entries:
            # hide Entry
            self.all_entries[col].grid_forget()

    def printEntries(self):
        print "-"*30
        for key in self.all_entries_vars:
            print "Entry #%d: %s" % ( key, self.all_entries_vars[key].get() )

    def printOptionMenus(self):
        print "-"*30
        for key in self.all_optionmenus_vars:
            print "OptionMenu #%d: %s" % ( key, self.all_optionmenus_vars[key].get() )


#----------------------------------------------------------------------

if __name__ == '__main__':
    App(Tk()).run()

enter image description here