-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypad.py
83 lines (70 loc) · 2.83 KB
/
keypad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""A module that make keypads"""
import tkinter as tk
class Keypad(tk.Frame):
"""The keypad class"""
def __init__(self, parent, keynames=[], columns=1, **kwargs):
super().__init__(parent, **kwargs)
self.keynames = keynames
self.init_components(columns)
def init_components(self, columns) -> None:
"""Create a keypad of keys using the keynames list.
The first keyname is at the top left of the keypad and
fills the available columns left-to-right, adding as many
rows as needed.
:param columns: number of columns to use
"""
options = {'sticky': tk.NSEW, 'padx':2, 'pady': 2}
for i ,keynames in enumerate(self.keynames):
row = i//columns
column = i % columns
if '&colspan=' in keynames:
keynames = keynames[0:-10]
button = tk.Button(self, text=keynames, width=5)
button.grid(row=row, column=column, **options)
self.rowconfigure(row, weight=1)
self.columnconfigure(column, weight=1)
def bind(self, sequence=None, func=None, add=None):
"""Bind an event handler to an event sequence."""
for child in self.children.values():
child.bind(sequence, func, add)
def __setitem__(self, key, value) -> None:
"""Overrides __setitem__ to allow configuration of all buttons
using dictionary syntax.
Example: keypad['foreground'] = 'red'
sets the font color on all buttons to red.
"""
for child in self.children.values():
child[key] = value
def __getitem__(self, key):
"""Overrides __getitem__ to allow reading of configuration values
from buttons.
Example: keypad['foreground'] would return 'red' if the button
foreground color is 'red'.
"""
return list(self.children.values())[0][key]
def configure(self, cnf=None, **kwargs):
"""Apply configuration settings to all buttons.
To configure properties of the frame that contains the buttons,
use `keypad.frame.configure()`.
"""
for child in self.children.values():
child.configure(cnf, **kwargs)
@property
def frame(self):
"""returns a reference to the superclass of the keypad"""
return super()
if __name__ == '__main__':
keys = list('789456123 0.') # = ['7','8','9',...]
root = tk.Tk()
frm = tk.Frame(root)
root.title("Keypad Demo")
keypad = Keypad(frm, keynames=keys, columns=3)
print([i['text'] for i in keypad.children.values()])
keypad['foreground'] = 'red'
print(keypad['foreground'])
keypad.bind('<Button>', lambda x: print(x.widget['text']))
keypad.frame.configure(background='blue')
keypad.configure(background='green')
keypad.pack()
frm.pack()
root.mainloop()