Как внедрить функциональные клавиши возврата и клавиатуры в свой калькулятор? - PullRequest
0 голосов
/ 01 октября 2019

Я использую Python Tkinter 3.7, чтобы сделать свой графический калькулятор. Для моей оценки мне нужно реализовать функциональные клавиши клавиатуры, которые будут также вводить цифры, и кнопку возврата, которая удаляет последнюю введенную цифру или оператор, однако я не мог понять, как сделать добавление привязки и возврата в tkinter. Я добавил аннотацию, чтобы показать, как я продвинулся в этом.

def cb(bs):           #cb=click on btn, bs=btn stuff
    global bd         # bd = stores & accum rcvd btn data
    bd=bd+str(bs) # bd=itself+the new bs(btn stuff)
    tv.set(bd)        # at some point clear out bd

def klr():
    global bd    # the accumulator of all btn data sent to cb()
    bd=''           # set bd to nothing
    tv.set(bd)   # tv var is bound to the text box: 'textvariable'

def eqf():
    global bd
    bd=eval(bd)
    tv.set(bd)
    bd=''

root = Tk()
root.title("Me Calculator")
tv=StringVar()
global bd    # bd = will store accumulated button data
bd=''           # bd = initially set to nothing



# textbox variable: tb
tb = Entry(root,font=('arial',18,'bold'),
           textvariable=tv,
           bd=15,
           insertwidth=3,
           bg='lightblue',
           justify='right').grid(columnspan=4)

tv.set('0.0')


# buttons section
btn7=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='7',
            command=lambda:cb(7)).grid(row=1,column=0)
btn8=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='8',
            command=lambda:cb(8)).grid(row=1,column=1)
btn9=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='9',
            command=lambda:cb(9)).grid(row=1,column=2)
divbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='/',
    command=lambda:cb('/')).grid(row=1,column=3)
####
btn4=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='4',
            command=lambda:cb(4)).grid(row=2,column=0)
btn5=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='5',
            command=lambda:cb(5)).grid(row=2,column=1)
btn6=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='6',
            command=lambda:cb(6)).grid(row=2,column=2)
mulbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='*',
            command=lambda:cb('*')).grid(row=2,column=3)

######




btn1=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='1',
            command=lambda:cb(1)).grid(row=3,column=0)
btn2=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='2',
            command=lambda:cb(2)).grid(row=3,column=1)
btn3=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='3',
            command=lambda:cb(3)).grid(row=3,column=2)
subtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='-',
            command=lambda:cb('-')).grid(row=3,column=3)


btn0=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='0',
            command=lambda:cb(0)).grid(row=4,column=0)
decbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='.',
            command=lambda:cb('.')).grid(row=4,column=1)
addbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='+',
            command=lambda:cb('+')).grid(row=4,column=2)

eqbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='=',
            command=lambda:eqf()).grid(row=4,column=3)

klrbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='Clear',
            command=lambda:klr()).grid(row=4,column=4)
root.mainloop()

Ответы [ 2 ]

0 голосов
/ 01 октября 2019

Вы можете использовать root.bind('<Return>', functionReturn) и root.bind('<BackSpace>', functionBackSpace)

И определить функцию с event в качестве аргумента:

def functionReturn(event): #your code for return here def functionBackSpace(event): #your code for backspace here

(см. https://stackoverflow.com/a/43920993/12141765 и https://stackoverflow.com/a/16996475/12141765 для подробных ответов)

0 голосов
/ 01 октября 2019

Может быть, это:

from tkinter import *;
def cb(bs):           #cb=click on btn, bs=btn stuff
    global bd         # bd = stores & accum rcvd btn data
    bd=bd+str(bs) # bd=itself+the new bs(btn stuff)
    tv.set(bd)        # at some point clear out bd

def klr():
    global bd    # the accumulator of all btn data sent to cb()
    bd=''           # set bd to nothing
    tv.set(bd)   # tv var is bound to the text box: 'textvariable'

def eqf():
    global bd
    bd=eval(bd)
    tv.set(bd)
    bd=''

def bck(): # function to operate
    global bd
    bd=str(bd)[0:-1] # We get bd from first car to preast
    tv.set(bd)

root = Tk()
root.title("Me Calculator")
tv=StringVar()
global bd    # bd = will store accumulated button data
bd=''           # bd = initially set to nothing



# textbox variable: tb
tb = Entry(root,font=('arial',18,'bold'),
           textvariable=tv,
           bd=15,
           insertwidth=3,
           bg='lightblue',
           justify='right').grid(columnspan=4)

tv.set('0.0')


# buttons section
btn7=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='7',
            command=lambda:cb(7)).grid(row=1,column=0)
btn8=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='8',
            command=lambda:cb(8)).grid(row=1,column=1)
btn9=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='9',
            command=lambda:cb(9)).grid(row=1,column=2)
divbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='/',
    command=lambda:cb('/')).grid(row=1,column=3)
####
btn4=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='4',
            command=lambda:cb(4)).grid(row=2,column=0)
btn5=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='5',
            command=lambda:cb(5)).grid(row=2,column=1)
btn6=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='6',
            command=lambda:cb(6)).grid(row=2,column=2)
mulbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='*',
            command=lambda:cb('*')).grid(row=2,column=3)

######




btn1=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='1',
            command=lambda:cb(1)).grid(row=3,column=0)
btn2=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='2',
            command=lambda:cb(2)).grid(row=3,column=1)
btn3=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='3',
            command=lambda:cb(3)).grid(row=3,column=2)
subtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='-',
            command=lambda:cb('-')).grid(row=3,column=3)


btn0=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='0',
            command=lambda:cb(0)).grid(row=4,column=0)
decbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='.',
            command=lambda:cb('.')).grid(row=4,column=1)
addbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='+',
            command=lambda:cb('+')).grid(row=4,column=2)

eqbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='=',
            command=lambda:eqf()).grid(row=4,column=3)

klrbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='Clear',
            command=lambda:klr()).grid(row=4,column=4)

# My addon
klrbtn=Button(root,padx=5,bd=8,
            fg="black",font=('arial',24,'bold'),
            text='Backs',
            command=lambda:bck()).grid(row=3,column=4)

root.mainloop()
...