Skip to content Skip to sidebar Skip to footer

Make Clear Button Backspace By One In Python Tkinter Calculator

I am making a calculator in python using tkinter. The calculator works very well apart from one thing. At the moment I have set the calculator to clear the display. I want to make

Solution 1:

Briefly: you get ValueError because you try to do int("clear") in

ifpressed== "clear": calcvalue = calcvalue - int(pressed) / 10

You can do this:

ifpressed== "clear": calcvalue = int(calcvalue/10.0)

Because you work only with integers and you use Python 2.x you can do even this:

ifpressed== "clear": calcvalue = calcvalue/10

In Python 2.x:

  • integer/integer gives always integer
  • float/integer and integer/float gives float

In Python 3.x:

  • integer/integer gives always float
  • integer//integer gives always integer

By the way:

You can this:

ifevent.x >10andevent.x <70 ...

replace with this:

if10< event.x <70 ...

And this:

ifpressed== 0orpressed== 1orpressed== 2orpressed== 3orpressed== 4orpressed== 5orpressed== 6orpressed== 7orpressed== 8orpressed== 9 :

ifpressed== "divide"orpressed== "times"orpressed== "minus"orpressed== "plus" :

with this:

if pressed in(0, 1, 2, 3, 4, 5, 6, 7, 8, 9):

if pressed in("divide", "times", "minus", "plus"):

It is more readable.

Post a Comment for "Make Clear Button Backspace By One In Python Tkinter Calculator"