user12532157
user12532157

Reputation:

TypeError : int() argument must be a string, a bytes-like object or a number, not 'Entry' in Python Tkinter

As I started creating a GUI calculator in Python using Tkinter in Python 3.8

I got stuck at a situation where i want to get the entry by the user in diffent entry box and the user is given four option wether to add , substract , multiply or divide

but as i was doing it was showing an error like this

int() argument must be a string, a bytes-like object or a number, not 'Entry'

I seriously dont know what is this type of error as i am a beginer in python and it is my first GUI project

the Entire Code is as follows:

    from tkinter import *
    from tkinter import messagebox

    import self as self

    global root

    top = Tk()
    top.title("Alex Mercer")


    def default():
         messagebox.showinfo("Guilty Message",
                   "Sorry! But this feature is not Yet Available Please Come Back after the available of the future update of this program\n Our E-mail Adress Is: [email protected]")


   def create_window():
      window = Toplevel(top)
      window.title("Message")
      default()


  def helpyou():
      window = Toplevel(top)
      window.title("Help")
      ourmessage = "Please Email Your Problem To our E-mail Adress : [email protected]"
default()


  w = 500
  h = 500
  menu = Menu(top)
  top.config(menu=menu)
  filemenu = Menu(menu)
  menu.add_cascade(label='File', menu=filemenu)
  filemenu.add_command(label="New", command=create_window)
  filemenu.add_command(label="Help", command=helpyou)
  filemenu.add_separator()
  filemenu.add_command(label="Exit", command=quit)
  editmenu = Menu(menu)
  menu.add_cascade(label="Edit", menu=editmenu)
  editmenu.add_command(label="Cut", accelerator="Ctrl+V", command=lambda:  
  self.editor.event_generate('<Control-x>'))
  editmenu.add_command(label = "Copy",accelerator="Ctrl+V", command=lambda: 
  self.noteBook.event_generate('<Control-c>'))
  editmenu.add_command(label = "Paste", accelerator="Ctrl+V", command=lambda: 
  self.noteBook.event_generate('<Control-v>'))

  Label(top, text = "Enter The First Number : ").grid(row = 0, padx = 10, pady = 20)
  Label(top, text = "    Enter The Second Number : ").grid(row = 1, padx = 22, pady = 30)
  firstvar = Entry(top)
  secondvar = Entry(top)
  firstvar.grid(row = 0, column = 1)
  secondvar.grid(row = 1, column = 1)
  top.geometry("{}x{}".format(w,h))

  def add():
      you = int(firstvar)
      you2 = int(secondvar)
      result = you + you2
      return result

  var = int(add())

  b = Button(top, text = "ADD", command = add)
  Label(top, text = "Your Result is : {}".format(var))


  mainloop()

Upvotes: 0

Views: 288

Answers (1)

Ali Beyit
Ali Beyit

Reputation: 414

You should first call .get() to your entry then convert it to integer. Simply do, you = int(firstvar.get()) see if it works. (I just found this info with quick google search, here is the source https://riptutorial.com/tkinter/example/30023/getting-int-from-entry-widget)

Upvotes: 1

Related Questions