Home > other >  How to insert something at the end of a entry in tkinter?
How to insert something at the end of a entry in tkinter?

Time:12-24

def inserter(entryblock, num):
    entryblock.insert(-1, num)

I am making a calculator and whenever I click the number buttons on the calculator, it always inserts them at the beginning but I want to insert them to the end of the entry. How do I insert them at the right instead of the left?

CodePudding user response:

The first argument to insert is the index at which to insert the text. Most often this is the index 0 (zero) to represent the beginning of the entry widget. The entry widget also supports the string literal "end" which represents the index immediately after the last character.

entryblock.insert("end", num)

For the canonical description of all supported indexes, see the section titled Indices in the tcl/tk man page for the Entry widget.

CodePudding user response:

You can use a variable to store the digits in the entry field and when you click another number it concatenates the variable and the number you enter. Since you enter an integer in the field and we need strings to concatenate we convert them into strings.


We need to use delete as the variable stores whatever is in the entry field for eg. if we click 23 it will show 232 as when you click 2 it stores 2 in the variable and then when you click 3 it prints 23 and 2 because we did not remove the previous 2 that was written there.

This should work:

def inserter(entryblock, num):
    digits=entryblock.get()
    entryblock.delete(0,END)
    entryblock.insert(0, str(digits) str(num))
  • Related