1

I am trying to make a simple calculator, I am using an entry widget to display the numbers, and buttons to type the numbers.

When I type numbers using the buttons, (btn1, btnadd, btn2), it should be like this in the entry widget 1+2 instead it is like this 2+1

I know mathematically they are the same, but it won't be the case with division or subtraction

My code:

from tkinter import *

root = Tk()

def add():
    entered.insert(0, '+')

def num_1():
    entered.insert(0, 1)

def num_2():
    entered.insert(0, 2)

entered = Entry(root)
entered.pack()
btn1 = Button(root, text='1', command=num_1).pack()
btn2 = Button(root, text='2', command=num_2).pack()
btn_add = Button(root, text='+', command=add).pack()

root.mainloop()

P.S I tried using pyautogui write function but the code was lagging and slow.

  • it happens because of the way `.insert()` works: it places the `+` at the start, instead of the end – Nick Aug 21 '22 at 13:48

3 Answers3

2

I'm new, hope this will help you!! ^^ Also, this is "smallest to change" solution: you just need to replace a few characters.

from tkinter import *

root = Tk()

def add():
    entered.insert(END, '+') # END instead of 0, it adds '+' at the end

def num_1():
    entered.insert(END, 1) # END instead of 0, it adds 1 at the end

def num_2():
    entered.insert(END, 2) # END instead of 0, it adds 2 at the end

entered = Entry(root)
entered.pack()
btn1 = Button(root, text='1', command=num_1).pack()
btn2 = Button(root, text='2', command=num_2).pack()
btn_add = Button(root, text='+', command=add).pack()

root.mainloop()

Thank you for making a fun to answer question! :D

stysan
  • 303
  • 1
  • 10
1

So the problem was that entered.insert(0, '+') the 0 is where its going to place the + so every time you were pushing the button you were placing the 1 and the 2 and the + at position 0

from tkinter import *

root = Tk()
i= 0

def add():
    global i
    entered.insert(i, '+')
    i += 1
def num_1():
    global i
    entered.insert(i, 1)
    i += 1

def num_2():
    global i
    entered.insert(i, 2)
    i += 1

entered = Entry(root)
entered.pack()
btn1 = Button(root, text='1', command=num_1).pack()
btn2 = Button(root, text='2', command=num_2).pack()
btn_add = Button(root, text=' +', command=add).pack()

root.mainloop()

so now you have the global i that will change the position of the placement...

PanosoikoGr
  • 125
  • 8
  • Will this work if I add more number buttons? – TheDiamondCreeper Aug 21 '22 at 13:56
  • this will work just every time you add a new button just add the global i and the i += 1. Your code is lacking in alot of ways for the calculator to work... – PanosoikoGr Aug 21 '22 at 14:12
  • It worked thank you, but do you know how to create the delete button, I want to delete just the last character without emulating the keypress? – TheDiamondCreeper Aug 21 '22 at 14:20
  • You don't have to remember the number of characters. You can use the string `"end"` when inserting: `entered.insert("end", 1)` – Bryan Oakley Aug 21 '22 at 15:24
  • @user17252227: I didn't downvote your answer, though your answer makes some fundamental mistakes that shouldn't be taught to new users. For example, in your answer `btn1`, `btn2`, and `btn_add` will all be `None` so there's really no point in assigning them. – Bryan Oakley Aug 21 '22 at 16:56
0

Ok this is how to delete

from asyncio.windows_events import NULL
from os import remove
from tkinter import *

root = Tk()
i= 0

def add():
    global i
    entered.insert(i, '+')
    i += 1
def num_1():
    global i
    entered.insert(i, 1)
    i += 1

def num_2():
    global i
    entered.insert(i, 2)
    i += 1
def delete():
    global i
    i -= 1
    entered.delete(i)



entered = Entry(root)
entered.pack()
btn1 = Button(root, text='1', command=num_1).pack()
btn2 = Button(root, text='2', command=num_2).pack()
btn_add = Button(root, text=' +', command=add).pack()
btn_rem = Button(root,text ='del', command=delete).pack()


root.mainloop()
PanosoikoGr
  • 125
  • 8