Home > Software engineering >  How to bind a key that prints in an entry box (tkinter)
How to bind a key that prints in an entry box (tkinter)

Time:04-04

I'm currently making a calculator with tkinter and I'm trying to implement an idea that I have, specifically, I'm trying to bind a number key and when pressed, inserts a number in my entry box but instead, the number gets inserted whenever I press an empty part of my window.

This is my code. I tried window.bind('<number>'), lambda event: output.insert(END, number) and this was the code that lead to the problem I described. Please help.

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()

CodePudding user response:

One of the way is to replace the following line

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

by

# 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))
  • Related