Jostimian
Jostimian

Reputation: 119

How to make an entry int in tkinter

i want to create a area of circle calculator because i have some free time so i tried to run it first in to the terminal and it works but when i decide to add a litte GUI well i got an error My code is this

from tkinter import *
screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")
def submit():
    global done
    pi = 3.14159265359
    an = int(pi * radius*radius)
    laod = Label(screen, text = "Hello" % (an))
    load.grid()
ask = Label(screen, text = "What is the radius of the circle?")
ask.pack()
radius = Entry(screen)
radius.pack()
done = Button(screen, text="submit", command = submit)
done.pack()
screen.mainloop()

and this is the error that i got

C:\Users\Timothy\Desktop>python aoc.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Timothy\AppData\Local\Programs\Python\Python37-32\lib\tkinter\_
_init__.py", line 1705, in __call__
    return self.func(*args)
  File "aoc.py", line 8, in submit
    an = int(pi * radius*radius)
TypeError: unsupported operand type(s) for *: 'float' and 'Entry'

C:\Users\Timothy\Desktop>

i try to add int() in to the entry but it dosent work

Upvotes: 2

Views: 297

Answers (1)

martineau
martineau

Reputation: 123463

You need to call radius.get() to get the Entry widget's current value. Here's some documentation on it.

There were some other errors and a typo in your code, so I fixed them and made it more PEP 8 - Style Guide for Python Code compliant.

Here's the result:

from math import pi
from tkinter import *


screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")

def submit():
    r = float(radius.get())
    an = pi * r**2
    load = Label(screen, text="Hello %f" % (an))
    load.pack()

ask = Label(screen, text="What is the radius of the circle?")
ask.pack()

radius = Entry(screen)
radius.pack()

done = Button(screen, text="submit", command=submit)
done.pack()

screen.mainloop()

Upvotes: 1

Related Questions