如何绑定在输入框中打印的键 (tkinter)

How to bind a key that prints in an entry box (tkinter)

我目前正在用 tkinter 制作一个计算器,我正在尝试实现我的一个想法, 具体来说,我正在尝试绑定一个数字键并在按下时在我的输入框中插入一个数字 但是,每当我按下 window.

的空白部分时,数字就会被插入

这是我的代码。 我尝试了 window.bind(''),lambda 事件:output.insert(END, number) 这是导致我描述的问题的代码。 请帮忙。

from tkinter import *from PIL import Image, ImageTk

window = Tk()window.geometry('300x440')window.title('Calculator - Trial Test')window.config(bg='#2a2a2b')window.resizable(0, 0)icon = ImageTk.PhotoImage(file='logo.png')window.iconphoto(True, icon)

def press_button(item):global expressionexpression = expression + str(item)input_text.set(expression)

def clear():global expressionexpression = ''input_text.set('')

def backspace():output.delete(len(output.get())-1, END)

def total2():global expressionresult = str(eval(expression))input_text.set(result)expression = ''

expression = ''

input_text = StringVar()

output = Entry(window, relief=RIDGE, borderwidth=2, font=('@Yu Gothic Light', 20, 'bold'), bg='#e3dcdc', textvariable=input_text,justify=RIGHT,)output.place(width=220,height=70,x=40, y=30)

label_text = Label(text='Licensed @Yeshua', font=('@Yu Gothic Light', 10), fg='#ffffff', bg='#2a2a2b')label_text.place(x=180, y=410)

clear = Button(window, text='C', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#f7f5f0',width=3, activeforeground='#000000', activebackground='#f7f5f0', command=clear)clear.place(x=48, y=120)

backspace = Button(window, text='⌫', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#f7f5f0',width=3, activeforeground='#000000', activebackground='#f7f5f0', command=backspace)backspace.place(x=100, y=120)

null2 = Button(window, bg='#f7f5f0', text='', font=('@Yu Gothic Light', 15, 'bold'), width=3, activebackground='#f7f5f0',relief=SOLID, borderwidth=2) # no cmd needed, non functional buttonnull2.place(x=152, y=120)

null = Button(window, bg='#f7f5f0', text='', font=('@Yu Gothic Light', 15, 'bold'), width=3, activebackground='#f7f5f0',relief=SOLID, borderwidth=2) # no cmd needed, non functional buttonnull.place(x=48, y=320)

add_button = Button(window, text='+', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#f7f5f0',width=3, activeforeground='#000000', activebackground='#f7f5f0',command=lambda: press_button('+'))add_button.place(x=205, y=270)

substract_button = Button(window, text='-', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2,bg='#f7f5f0', width=3, activeforeground='#000000', activebackground='#f7f5f0',command=lambda: press_button('-'))substract_button.place(x=205, y=220)

divide_button = Button(window, text='/', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2,bg='#f7f5f0', width=3, activeforeground='#000000', activebackground='#f7f5f0',command=lambda: press_button('/'))divide_button.place(x=205, y=170)

multiply_button = Button(window, text='', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2,bg='#f7f5f0', width=3, activeforeground='#000000', activebackground='#f7f5f0',command=lambda: press_button(''))multiply_button.place(x=205, y=120)

number_zero = Button(window, text='0', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(0))number_zero.place(x=100, y=320)

number_one = Button(window, text='1', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(1))number_one.place(x=48, y=271)

number_two = Button(window, text='2', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(2))number_two.place(x=100, y=271)

number_three = Button(window, text='3', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(3))number_three.place(x=152, y=271)

number_four = Button(window, text='4', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(4))number_four.place(x=48, y=220)

number_five = Button(window, text='5', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(5))number_five.place(x=100, y=220)

number_six = Button(window, text='6', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(6))number_six.place(x=152, y=220)

number_seven = Button(window, text='7', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(7))number_seven.place(x=48, y=170)

number_eight = Button(window, text='8', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(8))number_eight.place(x=100, y=170)

number_nine = Button(window, text='9', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#e3dcdc',width=3, activeforeground='#000000', activebackground='#e3dcdc',command=lambda: press_button(9))number_nine.place(x=152, y=170)

decimal_point = Button(window, text='.', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#f7f5f0',width=3, activeforeground='#000000', activebackground='#f7f5f0', command=lambda: press_button('.'))decimal_point.place(x=152, y=320)

total = Button(window, text='=', font=('@Yu Gothic Light', 15, 'bold'), relief=SOLID, borderwidth=2, bg='#f7f5f0',width=3, activeforeground='#000000', activebackground='#f7f5f0',command=total2)total.place(x=205, y=320)

window.bind('<Return>', lambda event: total2())window.bind('<number>', lambda event: output.insert(END, number)

window.mainloop()

其中一种方法是替换下面这行

# there is no <number> event
window.bind('<number>', lambda event: output.insert(END, number))

来自

# pressing one of the numbers will generate virtual event <<Number>>
window.event_add('<<Number>>', *'0123456789')
# bind virtual event <<Number>> to execute press_button()
window.bind('<<Number>>', lambda event: press_button(event.char))