Return Tkinter控制变量的引用

Return the reference of Tkinter control variable

我不是经验丰富的程序员,所以如果我问的是一个基本问题,请提前致歉。我正在使用 for 循环打印 tkinter 条目并尝试 return 这些条目对调用函数的引用。但是,当我尝试在条目中输入文本时,它总是 returns 空字符串?所以我的问题是是否可以 return 在 python 中引用控制变量?或者我使用了错误的方法?

    def data_entry_txtfield(self,rn,cn,pu,pd):
        # Creates the Entry to enter data - rn is the row and cn is column 
        # pd and pu are padding up and padding down
        entry = tk.StringVar()
        tk.Entry(self.inputlab,width=32,bg=entrycolor,textvariable=entry)
        entry.grid(column=cn,row=rn,pady=(pu,pd))
        return entry



    tbtlocationentry = self.data_entry_txtfield(9,4,0,12)
    text = tbtlocationentry.get()
    print(text)`

这段代码中有很多错误,比如你使用 self.data_entry_txtfield 调用函数的底线,但你不需要 self。除非你在 class 中使用。此外,该条目需要一个变量名才能更改其中的内容。我已经修复了一些错误,这是代码

def data_entry_txtfield(self,rn,cn,pu,pd):
        # Creates the Entry to enter data - rn is the row and cn is column 
        # pd and pu are padding up and padding down
        entry = tk.StringVar()
        ety = tk.Entry(self.inputlab,width=32,bg=entrycolor,textvariable=entry)
        ety.grid(column=cn,row=rn,pady=(pu,pd))
        return entry



    tbtlocationentry = self.data_entry_txtfield(9,4,0,12)
    text = tbtlocationentry.get()
    print(text)

我已经离开了自我。因为我假设你在 class

中使用它