1

So I am making a calculator, I want the user to press the delete key to delete what they had written in the entry field, But When I do it also presses the decimal('.') key in the keyboard, thus activating its own function, I am using the keyboard module for this

keyboard.on_press_key("delete", lambda _:CEd())

this should activate this function:-

def CEd():
    global counter
    if counter ==1:
       HentryPad.destroy()
       counter = 0
    else:
     field.delete(0,END)
     Clear1.config(relief=SUNKEN)
     Clear1.after(58, lambda: Clear1.config(relief=RAISED)) 

But it also activates this function:-

def Decimals():
global counter
if counter == 1:
    HentryPad.destroy()
    counter=0
    if field.get() != '':
        if upperField.get()=='OVERFLOW!!':
            upperAns.delete(0,END)
            upperField.delete(0, END)
            field.delete(0,END)
            field.insert(END,".")
        else:
            upperAns.delete(0,END)
            field.insert(END,".")
    else:
        upperAns.delete(0,END)
        field.insert(0,"0.")
else:
    if field.get() != '':
        if upperField.get()=='OVERFLOW!!':
            upperAns.delete(0,END)
            upperField.delete(0, END)
            field.delete(0,END)
            field.insert(END,".")
        else:
            upperAns.delete(0,END)
            field.insert(END,".")
    else:
        upperAns.delete(0,END)
        field.insert(0,"0.")
decimal.config(relief=SUNKEN,bg="black", fg="white")
decimal.after(58, lambda: decimal.config(relief=RAISED, bg="#262626", fg="cyan"))

I have keyed the Decimals function to the '.' key in the keyboard, but When i press the delete button on the keyboard, it also activates this function, here is the code for activating the decimals functions:-

keyboard.on_press_key(".", lambda _:Decimals())

There is nothing that is colliding with each other's function that I can spot, Pls help.

Duffy
  • 123
  • 7
  • 1
    if you notice in OS calculators there is no "Del" but "Backspace", and the "Del" is "Dot" . so one option that I can suggest is to support "Backspace" and leave the "Del" as "Dot". – ozs Aug 17 '21 at 07:36
  • I want to use del to delete all the characters from a field. I already use backspace to delete the last character entered by the user and I also use esc to delete all the things in the app(all the user entered characters). Btw is it true that 'del' actually works like a 'Dot' key in windows OS? – Duffy Aug 17 '21 at 08:33
  • on the numpad the "DEL" is a "DOT" (ascii code 46), but if you need to delete all then its another key with ascii code 127. – ozs Aug 17 '21 at 08:46
  • Why are you using the `keyboard` library when `tkinter` has bindings? – TheLizzard Aug 17 '21 at 09:12
  • The problem is, In tkinter in order for the built in key bindings to work, the widget should be in focus, But in my app, I have the need to switch around with a lot of widgets. This causes unwanted confusions and more workload on the processing as I am passing a lot functions to a single widget, Now I can overcome it by adding the bindings to all the widgets, but that would be just inefficient. I can say this because I have tried it, and I learnt that the hard way – Duffy Aug 17 '21 at 13:00
  • @Duffy Why not use `root.bind_all(...)`? That way all of the widgets will be binded with that one command. – TheLizzard Aug 17 '21 at 13:09

0 Answers0