0

So I'm trying to write some code that takes two user inputted values, compares them, and then tells the user if he/she input the correct values. (USL should be greater than LSL, pretty simple criteria) Here you can see how i get the entry from the user

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

This next bit of code is where I check the code

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

and finally here is where i define the check_limits function

    def check_limits(self):

    e6 = int(self.e6)
    e7 = int(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.text.delete(0.0, END)
    self.text.insert(0.0, message)

Currently I have the variables e6 and e7 forced as integers, but when i do this I get an error saying that int() arguments must be a string or a number, not "NoneType". Why would they be defined as null and not integers in this case?

Alternatively, if I don't force them to be integers then I always get "USL can't be less than...", regardless of whether e6 is greater or less than e7. Why is it doing this and how do I fix it?

Here's my entire code if any else of it is needed for the context of this question

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"""

    # get name
    Label(self, text = "First Name").grid(row = 0, column = 0, sticky = W, padx=5, pady=5)
    self.e1 = Entry(self).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).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).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).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).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).grid(row = 5, column = 1)    
    Label(self, text = "LSL").grid(row = 6, column = 0, sticky = W, padx=5, pady=5)       
    self.e7 = Entry(self).grid(row = 6, column = 1)


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

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

def check_limits(self):

    e6 = int(self.e6)
    e7 = int(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.text.delete(0.0, END)
    self.text.insert(0.0, message)








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

app = Application(root)

root.mainloop()
MisterJern
  • 45
  • 8
  • @Bryan Oakley I don't think the other question addresses the root of the OP's problem though (unless I'm missing something.) He or she is trying to use these variables to read the values from the Entry instead of using an IntVar. I had just finished typing an explanation of this when you blocked me from giving my answer. – saulspatz Nov 06 '15 at 01:58
  • @saulspatz: the root problem is the same -- `self.e6 = Entry(...).grid(...)` returns `None`, which is why the OP is getting a `NoneType` error. Using an `IntVar` or not is entirely unrelated. You can use the values without using an `IntVar`. If the user had searched for "NoneType object has no attribute" they likely would have found dozens if not hundreds of similar questions and answers. – Bryan Oakley Nov 06 '15 at 02:00
  • How can you get the values without using a textvariable? (I realize it doesn't have to be an IntVar, but that's unimportant.) – saulspatz Nov 06 '15 at 02:03
  • @saulspatz: there are multiple problems in the code, yes, but the question isn't "how do I fix my code". The question is "why am I getting a NoneType error". Here's a direct quote from the question: _"Why would they be defined as null and not integers in this case?"_ That question has been asked and answered dozens of times. One more question asking the same thing helps nobody. – Bryan Oakley Nov 06 '15 at 02:06
  • @saulspatz: _"How can you get the values without using a textvariable?"_: you call the `get` method of the widget. In this case it would be `self.e6.get()`, assuming `self.e6` is defined properly. – Bryan Oakley Nov 06 '15 at 02:08
  • I didn't realize the Entry had a `get` method, thanks. As to the other issue, we'll just have to agree to disagree. – saulspatz Nov 06 '15 at 02:10

1 Answers1

1

Your issue is this

self.e6 = Entry(self).grid(row = 5, column = 1)

When you want to use an assigned widget you can't pack, place or grid it. You need to change it to this

self.e6 = Entry(self)
self.e6.grid(row = 5, column = 1)

You are getting NoneType because of the assignment with the grid. Doing that is fine for static widgets like Labels and Buttons you don't plan on accessing. But when you want to get or assign a value to the widget, it needs to be the widget itself and not widget.grid()

Steven Summers
  • 5,079
  • 2
  • 20
  • 31