【问题标题】:Return the reference of Tkinter control variable返回 Tkinter 控制变量的引用
【发布时间】:2020-02-19 18:24:00
【问题描述】:

我不是一个经验丰富的程序员,所以如果我问一个基本问题,请提前道歉。我正在使用 for 循环打印 tkinter 条目并尝试将这些条目的引用返回给调用函数。但是,当我尝试获取在条目中键入的文本时,它总是返回空字符串?所以我的问题是是否可以在 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)`

【问题讨论】:

    标签: python-3.x variables tkinter return controls


    【解决方案1】:

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

    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)
    

    我已经离开了自我。因为我假设你在课堂上使用这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2021-09-15
      • 2018-09-24
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多