我正在使用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()
答案 0 :(得分:0)
也许是这样:
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()
答案 1 :(得分:0)
您可以使用
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)
答案 2 :(得分:0)
我刚刚使用back()
在clear下定义了str[:-1]
函数。 -1只是字符串中的最后一个字符,因为它是从另一端开始的。
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)