Aravind Selvan
Aravind Selvan

Reputation: 61

How to press button from keyboard instead of using mouse in tkinter?

I'm new to tkinter and I'm trying to create a calculator similar to windows calculator for practicing. I have created a calculator UI using tkinter.

If I press any key from keyboard, the button should be pressed in the calculator window similar to windows calculator. I have given justify as left for l1. But it is aligned in center only. I don't know where I'm wrong. Also I need to know how to create a sliding menu in tkinter.

Any ideas will be really helpful.

Below is the code of my calculator UI.

#Importing required libraries
from tkinter import *
import math
import fractions
import tkinter.font as font
from PIL import ImageTk, Image

#Creating instance for window
win = Tk()

#Function for reset
def reset(dummy=None):
    e1.delete(0, 'end')
    e2.delete(0, 'end')

#Function for backspace
def backspace():
    x = str(e2.get())
    e2.delete(0, END)
    e2.insert(0, x[:-1])

#Function for button click
def click(number):
    x = e2.get()
    e2.delete(0, END)
    e2.insert(0, str(x) + str(number))

#Giving title to window
win.title('Calculator')
#Customizing main=window
win.geometry('330x443')
win.configure(bg='#e8e8e8')
win.resizable(0,0)

#Defining Font style
l1_font = font.Font(family='Georgia', size=15)
e1_font = font.Font(family='Helvetica', size=15)
e2_font = font.Font(family='Helvetica', size=35)
b_font = font.Font(family='Helvetica', size=15)

#Binding enter and escape to window

win.bind('<Return>')
win.bind('<Escape>', reset)

#Frame1
f1 = Frame(win, bg='#e6e6e6')
f1.pack()

#Frame1 - Label1
l1 = Label(f1, text='Calculator', justify='left', anchor='w', justify='left', bg='#e6e6e6', pady=20)
l1['font'] = l1_font
l1.pack()

#Frame2
f2 = Frame(win, bg='#e6e6e6')
f2.pack()

#Frame2 - Entry1
e1 = Entry(f2, width = 65, justify = 'right', bg='#e6e6e6', validate="key", relief=FLAT)
#e1['validatecommand'] = (e1.register(entry_validation),'%P','%d')
e1['font'] = e1_font
e1.pack()

#Frame2 - Entry2
e2 = Entry(f2, width = 65, justify = 'right', bg='#e6e6e6', validate="key", relief=FLAT)
#e2['validatecommand'] = (e2.register(entry_validation),'%P','%d')
e2['font'] = e2_font
e2.pack()

#Frame3
f3 = Frame(win, bg='#e6e6e6')
f3.pack()

#Frame3 - Buttons
button1 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(1))
button2 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(2))
button3 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(3))
button4 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(4))
button5 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(5))
button6 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(6))
button7 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(7))
button8 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(8))
button9 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(9))
button0 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click(0))

button_add = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=lambda: click('+'))
button_sub = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=lambda: click('-'))
button_mult = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=lambda: click('*'))
button_div = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=lambda: click('/'))

button_dot = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=lambda: click('.'))
button_enter = Button(f3, height =3, width =10, relief=FLAT, bg='#9aafed')
button_clear = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=reset)
button_bs = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=backspace)

button_space = Button(f3, height =3, width =10, relief=FLAT, bg='#f7f7f7', command=lambda: click(' '))
button_na2 = Button(f3, height =3, width =10, relief=FLAT, bg='white', command=reset)

#Shoving buttons into window

#Frame3 - Buttons - Row 1
button_clear.grid(row = 0, column = 0, padx=0.75, pady=0.75)
button_div.grid(row = 0, column = 1, padx=0.75, pady=0.75)
button_mult.grid(row = 0, column = 2, padx=0.75, pady=0.75)
button_bs.grid(row = 0, column = 3, padx=0.75, pady=0.75)

#Frame3 - Buttons - Row 2
button7.grid(row = 1, column = 0, padx=0.75, pady=0.75)
button8.grid(row = 1, column = 1, padx=0.75, pady=0.75)
button9.grid(row = 1, column = 2, padx=0.75, pady=0.75)
button_sub.grid(row = 1, column = 3, padx=0.75, pady=0.75)

#Frame3 - Buttons - Row 3
button4.grid(row = 2, column = 0, padx=0.75, pady=0.75)
button5.grid(row = 2, column = 1, padx=0.75, pady=0.75)
button6.grid(row = 2, column = 2, padx=0.75, pady=0.75)
button_add.grid(row = 2, column = 3, padx=0.75, pady=0.75)

#Frame3 - Buttons - Row 4
button1.grid(row = 3, column = 0, padx=0.75, pady=0.75)
button2.grid(row = 3, column = 1, padx=0.75, pady=0.75)
button3.grid(row = 3, column = 2, padx=0.75, pady=0.75)
button_space.grid(row = 3, column = 3, padx=0.75, pady=0.75)

#Frame3 - Buttons - Row 5
button_na2.grid(row = 4, column = 0, padx=0.75, pady=0.75)
button0.grid(row = 4, column = 1, padx=0.75, pady=0.75)
button_dot.grid(row = 4, column = 2, padx=0.75, pady=0.75)
button_enter.grid(row = 4, column = 3, padx=0.75, pady=0.75)

win.mainloop()

Upvotes: 1

Views: 302

Answers (1)

Tomπ
Tomπ

Reputation: 195

For capturing keyboard events try code below. Example taken from effbot.org

def key(event):
print("pressed", repr(event.char))

frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.focus_set()
frame.pack()

You used justify twice. Maybe this is the problem.

l1 = Label(f1, text='Calculator', justify='left', anchor='w', justify='left', bg='#e6e6e6', pady=20)

To create sliding menu you can try using scale widget. More info again on effbot.org

Scrollbar example effbot.org

from tkinter import *

root = Tk()

left_frame = Frame(root, height=200, width=200, background='green')
left_frame.grid(row=0, column=0)
right_frame = Frame(root, height=200, width=200)
right_frame.grid(row=0, column=1)

scrollbar = Scrollbar(right_frame)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(right_frame, yscrollcommand=scrollbar.set)
for i in range(1000):
    listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)

scrollbar.config(command=listbox.yview)

mainloop()

Upvotes: 2

Related Questions