Home > database >  Tkinter entry widget input are written backwards
Tkinter entry widget input are written backwards

Time:08-21

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.

CodePudding user response:

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...

CodePudding user response:

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()
  • Related