-1

So I'm very new to python and programming in general. I made this program who determines which person is heavier. Don't worry about why it's just a running gag between some friends. Anyways, I've been trying to put it into a GUI with Tkinter but am having issues with the entry boxes and converting them into integers. I've tried using IntVar, entry_box = int(entry_box), and entry_box = float(entry_box). Nothing seems to work and I'm getting errors every time.

from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("640x640+0+0")

# LABELS
heading = Label(root, text="Welcome to the Who's More Fat Program", font=("arial", 40, "bold"), 
fg="steelblue").pack()
person_1 = Label(root, text="Enter first persons name: ", font=("arial",20, "bold"), 
fg="black").place(x=10, y=200)
weight_1 = Label(root, text="Enter first persons weight: ", font=("arial",20, "bold"), 
fg="black").place(x=10, y=230)
person_2 = Label(root, text="Enter second persons name: ", font=("arial",20, "bold"), 
fg="black").place(x=10, y=290)
weight_2 = Label(root, text="Enter second persons weight: ", font=("arial",20, "bold"), 
fg="black").place(x=10, y=320)


# TEXT BOXES

entry_box1 = StringVar
entry_box2 = IntVar
entry_box3 = StringVar
entry_box4 = IntVar


entry_box1 = Entry(root, width=25, bg="white").place(x=350, y=210)
entry_box2 = Entry(root, width=25, bg="white").place(x=370, y=240)
entry_box3 = Entry(root, width=25, bg="white").place(x=395, y=300)
entry_box4 = Entry(root, width=25, bg="white").place(x=410, y=330)

entry_box1 = StringVar
entry_box2 = IntVar
entry_box3 = StringVar
entry_box4 = IntVar



def calc():
    if entry_box2.get > entry_box4.get:
        answer.insert(entry_box1.get)
        answer.insert(" Is the Fattest")
    else:
        answer.insert(entry_box3.get)
        answer.insert(" Is the Fattest")





# CALCULATE BUTTON

calculate = Button(root, text="Calculate the Fattest!", font=("arial",10, "bold"), bg="white", 
command=calc).place(x=525, y=205)
answer = Label(root, text="Answer: ", font=("arial",10, "bold"), bg="white",).place(x=400, y=400)

Traceback

Tkinter callback Traceback (most recent call last): 
FileC:\Users\Liam\AppData\Local\Programs\Python\Python37\lib\
tkinter_init_.py", line 1705, in call return self.func(*args) 
File "C:/Users/Liam/PycharmProjects/untitled/window.py", line 30, 
in calc if entry_box2.get > entry_box4.get: 
AttributeError: 'NoneType' object has no attribute 'get' ```
yabberth
  • 507
  • 4
  • 9
Liam Welsh
  • 194
  • 8
  • Didn't copy root.mainloop() but it is there at the end. – Liam Welsh Dec 22 '19 at 18:26
  • "Nothing seems to work" - what and how exactly doesn't work? "I'm getting errors every time" - please include the full traceback in your post – ForceBru Dec 22 '19 at 18:29
  • @ForceBru Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Liam\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/Liam/PycharmProjects/untitled/window.py", line 30, in calc if entry_box2.get > entry_box4.get: AttributeError: 'NoneType' object has no attribute 'get' – Liam Welsh Dec 22 '19 at 18:48
  • @ForceBru I've already included what I've tried in the post. Thanks for trying to help. – Liam Welsh Dec 22 '19 at 18:50
  • 1
    I'm getting a completely different error with Python 3.7.1 on the same line: `TypeError: '>' not supported between instances of 'function' and 'function'` – ForceBru Dec 22 '19 at 18:58
  • @ForceBru I've gotten that error before too. I've also gotten TypeError: '>' not supported between instances of 'NoneType' and 'NoneType' as well. – Liam Welsh Dec 22 '19 at 19:06
  • There are many, sort of, unexpected behaviours in this code. 1) in `entry_box1 = Entry(root, width=25, bg="white").place(x=350, y=210)` the method `place` will return `None`, so all `entry_box`es will be `None`; 2) `entry_box1 = StringVar` and friends simply add aliases to the classes `StringVar` and `IntVar`, and none of the `entry_box`es will be _instances_ of any of these classes; 3) `entry_box2.get` does not call the method, so you end up comparing _methods_, not the results of their invocation, but because of #2, you're comparing _functions_, not methods.; – ForceBru Dec 22 '19 at 19:19
  • 4) the method `get` isn't called in `answer.insert(entry_box1.get)` either; 5) `answer` will be `None` as well (see #1); 6) after fixing #5, you'll see that the method `answer.insert` doesn't exist: `answer.insert(entry_box3.get()) AttributeError: 'Label' object has no attribute 'insert'` – ForceBru Dec 22 '19 at 19:20

1 Answers1

0

As already said by ForceBru, there are many different errors in the proposed code. So maybe the easiest answer is to provide a equivalent working code, and let you analyse and compare the answer with your own code:

from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("800x600")

# LABELS
font1, font2, font3 = "arial 30 bold", "arial 18 bold", "arial 18"
Label(root, text="Welcome to the Who's More Fat Program", font=font1,
      fg="steelblue").place(x=0, y=0)
Label(root, text="Enter first persons name: ", font=font2,
      fg="black").place(x=10, y=200)
Label(root, text="Enter first persons weight: ", font=font2,
      fg="black").place(x=10, y=240)
Label(root, text="Enter second persons name: ", font=font2,
      fg="black").place(x=10, y=300)
Label(root, text="Enter second persons weight: ", font=font2,
      fg="black").place(x=10, y=340)

# TEXT BOXES
entry_box1 = Entry(root, width=25, font=font3, bg="white")
entry_box1.place(x=400, y=200)
entry_box2 = Entry(root, width=25, font=font3, bg="white")
entry_box2.place(x=400, y=240)
entry_box3 = Entry(root, width=25, font=font3, bg="white")
entry_box3.place(x=400, y=300)
entry_box4 = Entry(root, width=25, font=font3, bg="white")
entry_box4.place(x=400, y=340)

def calc():
    if float(entry_box2.get()) > float(entry_box4.get()):
        answer.delete(0, END)
        answer.insert(0, entry_box1.get() + " is the Fattest")
    else:
        answer.delete(0, END)
        answer.insert(0, entry_box3.get() + " is the Fattest")

# CALCULATE BUTTON

Button(root, text="Calculate the Fattest!", font=font2,
                   bg="white", command=calc).place(x=5, y=390)
Label(root, text="Answer:", font=font2).place(x=290, y=400)
answer = Entry(root, font=font3, width=25, bg="white")
answer.place(x=400, y=400)
sciroccorics
  • 2,357
  • 1
  • 8
  • 21