如何在我的计算器中实现退格键和键盘功能键?
How do I implement a backspace and keyboard function keys into my calculator?
我正在使用 Python Tkinter 3.7 制作我的 GUI 计算器。对于我的评估,我需要实现也将输入数字的键盘功能键和一个删除最后输入的数字或运算符的退格键,但是,我无法弄清楚如何在 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()
也许是这样:
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()
你可以使用
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
(详见 and )
我刚刚在clear下定义了一个back函数,它使用str[:-1] -1只是str的END,因为它从另一边开始
def back():
global bd
bd = bd[:-1]
tv.set(bd)
然后我做了按钮。顺便说一句,我使用的按钮网格看起来不是最好的
backspace = Button(root, padx=5, bd=8,
fg="black", font=('arial', 24, 'bold'),
text='Back',
command=lambda: back()).grid(row=3, column=4)
我正在使用 Python Tkinter 3.7 制作我的 GUI 计算器。对于我的评估,我需要实现也将输入数字的键盘功能键和一个删除最后输入的数字或运算符的退格键,但是,我无法弄清楚如何在 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()
也许是这样:
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()
你可以使用
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
(详见
我刚刚在clear下定义了一个back函数,它使用str[:-1] -1只是str的END,因为它从另一边开始
def back():
global bd
bd = bd[:-1]
tv.set(bd)
然后我做了按钮。顺便说一句,我使用的按钮网格看起来不是最好的
backspace = Button(root, padx=5, bd=8,
fg="black", font=('arial', 24, 'bold'),
text='Back',
command=lambda: back()).grid(row=3, column=4)