I am making a calculator. I want to add the backspace button. When I try it, it does show backspace in the console but it wont insert the backspaced value (c) in the entry box. [enter image description here]
I want the program to allow back space. Just the most right value and show the rest in the entry box. For example, if I type 98578 in the entry box, a backspace should change it to 9857. Can someone help?
from tkinter import *
import math
window = Tk()
window.minsize(width=300, height=350)
window.title("Scientific Calculator")
def back_space():
b = input.get()
c = b "\b"
input.delete(0, END)
print(c)
input.insert(INSERT, c)
Main_Button = Label(text="CALCULATOR", font=("Times New Roman", 16, "bold"))
Main_Button.pack()
Main_Button.place(x=80,y=5)
input = Entry(window, bd = 10, width=22, fg='black', bg='light blue')
input.place(x=82, y=32)
print(input.get())
button = Button(window, text="⌫", width=3, height=2, fg='black', bg='light
blue',command=back_space)
button.pack()
button.place(x=200, y=60)
window.mainloop()
CodePudding user response:
in this case it makes sense to use string indicies.
For this to work update your back_space function to this:
def back_space():
b = input.get()
input.delete(0, END)
input.insert(0, b[0:-1])
This basically takes the value from the entry widget and crops of the last character (or number) of the string. This is achived using string indicies.
With these string indicies you can select a certain part of a string. In this case I selected everything of string b from index 0 (first character) to the -1th caracter (this is then the first character from the end). Lastly the second index is not included. Therefore the string is cropped by one digit.
CodePudding user response:
Try this:
def back_space():
b = input.get()
c = b[:-1]
input.delete(0, END)
print(c)
input.insert(INSERT, c)
Hope this help
CodePudding user response:
Inserting a backspace character is not the solution. What the backspace key actually does is delete the character before the insertion point. You need to get the index of the insertion cursor and then delete the character immediately before it.
def back_space():
insert = int(input.index("insert"))
if insert > 0:
input.delete(insert-1)
If you always want to delete the last character regardless of the insertion point, use the index of the last character rather than the index of the insertion point:
def back_space():
end = int(input.index("end"))
if end > 0:
input.delete(end-1)
The advantage to using the delete
method in this way, rather than getting all of the text, trimming it, and then replacing the original text with the new as shown in other answers, is that this method preserves the location of the insert cursor as well as the selection. Those two get lost of you delete and then replace the entire contents of the entry widget.
CodePudding user response:
You can use the pyautogui module to simulate the backspace key press
Install it on windows using
pip install pyautogui
Import it to your code
import pyautogui
then create the 'back_space' function
def back_space():
pyautogui.press('backspace')
Full code:
from tkinter import *
import math
import pyautogui
window = Tk()
window.minsize(width=300, height=350)
window.title("Scientific Calculator")
def back_space():
pyautogui.press('backspace')
Main_Button = Label(text="CALCULATOR", font=("Times New Roman", 16, "bold"))
Main_Button.pack()
Main_Button.place(x=80,y=5)
input = Entry(window, bd = 10, width=22, fg='black', bg='light blue')
input.place(x=82, y=32)
print(input.get())
button = Button(window, text="⌫", width=3, height=2, fg='black', bg='light blue',command=back_space)
button.pack()
button.place(x=200, y=60)
window.mainloop()