![](/img/trans.png)
[英]how do i get keystrokes outside of program and of arrow keys, backspace, enter, etc in python?
[英]How do I implement a backspace and keyboard function keys into my calculator?
我正在使用 Python Tkinter 3.7 来制作我的 GUI 计算器。 对于我的评估,我需要实现键盘 function 键,该键也将输入数字和一个退格按钮,用于删除最后一个数字或输入的运算符,但是,我无法弄清楚如何在 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
作为参数定义 function:
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 )
我刚刚在 clear 下定义了一个 back function 使用 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.