0

I am trying to make a simple calculator that gives square and square root with using Python 2.7.10 with GUI but it doesn't work I can't figure out what is the problem. I receive this error:

> Exception in Tkinter callback Traceback (most recent call last): File
> "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in call return
> self.func(*args) File "C:/Users/Ali/Desktop/simplecalc.py", line 5, in
> do_sqrt root = x**0.5 TypeError: unsupported operand type(s) for ** or
> pow(): 'str' and 'float'
import Tkinter    
import tkMessageBox

def do_sqrt():
    root = x**0.5
    tkMessageBox.showinfo("Square Root = ", x)

def do_square():
    square = x**2
    tkMessageBox.showinfo("Square = ", x)

main_window = Tkinter.Tk()
main_window.title("Simple Calc")
number_input = (Tkinter.Entry(main_window))
x = number_input.get()
button_sqrt = Tkinter.Button(main_window, text = "Square Root", command = do_sqrt)
button_sqrt.pack()
button_square = Tkinter.Button(main_window, text = "Square", command =     do_square)
button_square.pack()
number_input.pack()

main_window.mainloop()
4
  • "It doesn't work" how exactly? Errors? Incorrect results? Commented Jan 29, 2016 at 21:46
  • Welcome to Stack Overflow! Please add more details to clarify the question you are asking and provide necessary context. Commented Jan 29, 2016 at 21:47
  • Window pops and I enter a number to the box then click to square root button or square button but then I get this error. Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in call return self.func(*args) File "C:/Users/Ali/Desktop/simplecalc.py", line 5, in do_sqrt root = x**0.5 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float' Commented Jan 29, 2016 at 21:49
  • @AliBeyit Please edit your question to add those details. They're very hard to read in the comments. Commented Jan 29, 2016 at 21:50

2 Answers 2

1

You are reading the string from the entry and trying to perform some math. And you are not using root and square variables.

import Tkinter
import tkMessageBox


def do_sqrt():
    root = float(number_input.get())**0.5
    tkMessageBox.showinfo("Square Root = ", root)


def do_square():
    square = float(number_input.get())**2
    tkMessageBox.showinfo("Square = ", square)

main_window = Tkinter.Tk()
main_window.title("Simple Calc")
number_input = Tkinter.Entry(main_window)
button_sqrt = Tkinter.Button(main_window, text="Square Root", command=do_sqrt)
button_sqrt.pack()
button_square = Tkinter.Button(main_window, text="Square", command=do_square)
button_square.pack()
number_input.pack()

main_window.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thanks god. I thought I tried this but forgot to write float around the number_input.get(). Thanks a lot for the help
you are welcome. I would also suggest to stick with OOP approach in writing in tkinter, it's shorter and easier to reuse and structure the gui app.
1

The line x = number_input.get() will return a string, but you are trying to use it as a number. Use the line x = float(number_input.get()) instead. This would have been clear if you included the error printout:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'

Also, it would be much better to avoid global variables, but that's another issue for another day.

3 Comments

Thanks for the advice but I tried it also. It gives this error. ValueError: could not convert string to float:
Yeah, if your input is empty or has non-numeric values, it's not going to be able to be converted into a float.
But I input numeric values still gives the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.