How to create a function to delete a character in a label tkinter python?

Question:

I’m about to finish a calculator in tkinter python. One of the last things I need to do is create a button which will delete one character at a time from the label. I’ve already created a button which will clear everything, but there all I did was var.set(" ") to essentially replace everything with a space. I’m not sure how to make a function which will delete 1 character at a time which is what I need to do.
This is the code for the button so far. I tried inserting a backspace but realised that wouldn’t work.

delete = Button(e, text = "C",width=4, command = lambda: insert("b"), font = "Helvetica 50",highlightbackground='#737373')
delete.place(x = 228, y = 301)

Any help on how to create a function like this is appreciated.

Asked By: Ippos

||

Answers:

A quick way would be to say:

delete = Button(..., command=lambda: var.set(var.get()[:-1]), ...)

A lengthier way would be to create a descriptive function and remove the use of StringVar:

def func():
    text = label.cget('text')[:-1] # Get the text of the label and slice till the last letter
    label.config(text=text) # Set the text of label to the new text

delete = Button(..., command=func, ...)
Answered By: Delrius Euphoria

Another quick way:

b=Button(text=’delete’,command=lambda:(Label_Name).config(text=(Label_Name).cget(‘text’)[:-1]))
b.pack()

Answered By: sunil
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.