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.
CodePudding user response:
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, ...)