无法插入用户输入值,因为 tkinter 中 "str object has no attribute get"

Can't insert a user input value because "str object has no attribute get" in tkinter

所以其他几个人也问过类似的问题,但我认为他们不一定适用于我的情况。我正在编写一个程序,它最终将接受用户输入,检查以确保它们正确,然后将其全部保存到一个文件中。目前,我将所有用户输入传递到 tkinter 中的一个文本框,这样我就可以只保存那个文本框中的任何内容。我正在使用 for 循环遍历我拥有的 7 个用户输入字段,然后将它们插入到文本框中。

def submit(self):
    """ submits user data up to input data section of GUI and checks USL vs LSL"""
    e6 = IntVar(self.e6)
    e7 = IntVar(self.e7)

    if e6 > e7:
        message = "Limits are good"
    else:
        message = "USL can't be less than LSL, please re-enter USL and LSL"

    self.checklimits.delete(0.0, END)
    self.checklimits.insert(0.0, message)


    x = 1
    for x in range (1, 8):
       xname = "self.e" + str(x)
       entry = xname.get()
       if entry:
            self.checktext.insert(END, entry + "\n")
       x = x+1

我想在 for 循环中获取 x 值并最终得到类似 "self.e#.get()" 的结果,因为这就是我定义用户条目的方式,请参见下面的示例:

 def create_widgets(self):
    """create widgets for user inputted data"""      
    # creates a text widget next to the entries that displays what the user has input
    Label(self, text = "Do these values you entered seem correct?").grid(row = 0, column = 4, sticky = W, padx = 5, pady = 5)
    self.checktext = Text(self, width =15, height = 42, wrap = WORD)
    self.checktext.grid(row = 1, rowspan = 10, column = 4, sticky = W, padx =5, pady =5)

    # get name
    Label(self, text = "First Name:").grid(row = 0, column = 0, sticky = W, padx=5, pady=5)
    self.e1 = Entry(self)
    self.e1.grid(row = 0, column = 1)       
    Label(self, text = "Last Name:").grid(row = 1, column = 0, sticky = W, padx=5, pady=5)
    self.e2 = Entry(self)
    self.e2.grid(row = 1, column = 1)

虽然现在,python 没有将该条目识别为一个条目,并告诉我“'str' 对象没有属性 'get'”

所以第一,为什么我不能 "get" 一个字符串值,第二,我怎样才能 python 识别我以前定义的条目?这是我的整个代码 context

from Tkinter import *

class Application(Frame):
""" A SPC program that takes user input and saves the file """
def __init__(self,master):
    """ initializes the frame """
    Frame.__init__(self,master)
    self.grid()
    self.create_widgets()     

def create_widgets(self):
    """create widgets for user inputted data"""      
    # creates a text widget next to the entries that displays what the user  has input
    Label(self, text = "Do these values you entered seem correct?").grid(row = 0, column = 4, sticky = W, padx = 5, pady = 5)
    self.checktext = Text(self, width =15, height = 42, wrap = WORD)
    self.checktext.grid(row = 1, rowspan = 10, column = 4, sticky = W, padx =5, pady =5)

    # get name
    Label(self, text = "First Name:").grid(row = 0, column = 0, sticky = W, padx=5, pady=5)
    self.e1 = Entry(self)
    self.e1.grid(row = 0, column = 1)       
    Label(self, text = "Last Name:").grid(row = 1, column = 0, sticky = W, padx=5, pady=5)
    self.e2 = Entry(self)
    self.e2.grid(row = 1, column = 1)

    # get work order
    Label(self, text = "Work Order Number:").grid(row = 2, column = 0, sticky = W, padx=5, pady=5)
    self.e3 = Entry(self)
    self.e3.grid(row = 2, column = 1)

    # get todays date
    Label(self, text = "Todays Date:").grid(row = 3, column = 0, sticky = W, padx=5, pady=5)
    self.e4 = Entry(self)
    self.e4.grid(row = 3, column = 1)

    # get bubble number
    Label(self, text = "Bubble Number:").grid(row = 4, column = 0, sticky = W, padx=5, pady=5)
    self.e5 = Entry(self)
    self.e5.grid(row = 4, column = 1)

    # get USL and LSL
    Label(self, text = "USL:").grid(row = 5, column = 0, sticky = W, padx=5, pady=5)        
    self.e6 = Entry(self)
    self.e6.grid(row = 5, column = 1)    
    Label(self, text = "LSL:").grid(row = 6, column = 0, sticky = W, padx=5, pady=5)       
    self.e7 = Entry(self)
    self.e7.grid(row = 6, column = 1)                       

    """# button to check USL is higher than LSL
    self.button7 = Button(self)
    self.button7["text"] = "Check Limits"
    self.button7["command"] = self.check_limits
    self.button7.grid(row = 6, column = 2, sticky = W, padx=5, pady=5)"""

    # button to submit user entered values up to the input data values portion of the gui
    self.button6 = Button(self)
    self.button6["text"] = "Submit"
    self.button6["command"] = self.submit
    self.button6.grid(row = 5, column = 2, sticky = W, padx=5, pady=5)

    # creates a spot to dictate whether USL and LSL are correct
    self.checklimits = Text(self, width = 20, height = 2, wrap = WORD)
    self.checklimits.grid(row = 6, column = 3, sticky = W, padx = 5)

    """ #adds a scroll bar to the data input text box
    scrollbar = Scrollbar(self)
    scrollbar.pack(side=RIGHT, fill=Y) """

    # get User Input Data values
    Label(self, text = "Enter Results:").grid(row = 7, column = 0, sticky = W, padx=5, pady=5)        
    self.e8 = Text(self, width = 15, height = 30)
    self.e8.grid(row = 7, column = 1)     

    """ def check_limits(self):
    checks to see if the USL is greater than the LSL 

    e6 = IntVar(self.e6)
    e7 = IntVar(self.e7)

    if e6 > e7:
        message = "Limits are good"
    else:
        message = "USL can't be less than LSL, please re-enter USL and LSL"

    self.checklimits.delete(0.0, END)
    self.checklimits.insert(0.0, message)"""

def submit(self):
    """ submits user data up to input data section of GUI and checks USL vs LSL"""
    e6 = IntVar(self.e6)
    e7 = IntVar(self.e7)

    if e6 > e7:
        message = "Limits are good"
    else:
        message = "USL can't be less than LSL, please re-enter USL and LSL"

    self.checklimits.delete(0.0, END)
    self.checklimits.insert(0.0, message)


    x = 1
    for x in range (1, 8):
       xname = "self.e" + str(x)
       entry = xname.get()
       if entry:
            self.checktext.insert(END, entry + "\n")
       x = x+1                              

root = Tk()
root.title("SPC Input Program")
root.geometry("700x750")

app = Application(root)

root.mainloop()

您似乎假设 e6 = IntVar(self.e6) 正在获取条目小部件的值并将其转换为 int。那不是它在做什么。它正在创建一个名为 e6 的新变量,该变量初始化为零,并且 self.e6 作为 "master"。

如果您想从条目小部件获取值并将其转换为整数,请在条目小部件上使用 get 方法。

e6 = int(self.e6.get())
e7 = int(self.e7.get())

导致 "str object has no method get" 的问题是这段代码:

xname = "self.e" + str(x)
entry = xname.get()

这是一种糟糕的编码方式。如您所见,它不会起作用。您可以使用技巧使其工作,但一个好的经验法则是永远不要尝试动态创建这样的变量。

如果您想遍历条目小部件,请将它们放在列表或元组中。例如:

for widget in (self.e1, self.e2, self.e3, self.e4,
              self.e5, self.e6, self.e7, self.e8):
    entry = widget.get()
   if entry:
       self.checktext.insert(END, entry + "\n")
   ...