-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestKeyboard.py
More file actions
121 lines (108 loc) · 4.26 KB
/
TestKeyboard.py
File metadata and controls
121 lines (108 loc) · 4.26 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# TestKeyboard.py
# Python3 scalable Keyboard with Combobox
from tkinter import *
from tkinter import ttk
from functools import partial
class Keyboard: # scalable Keyboard with Combobox
def __init__(self, parent, Inputvals, Title='Keyboard', Btnsize=28, Font='Helvetica 10'):
self.top = Toplevel(parent)
self.top['bg'] = '#AAAAAA' # window background
self.top.option_add('*background','#DDDDDD') # widget background
self.top.title(Title)
#self.top.overrideredirect(True) # without titlebar
self.top.geometry(str(Btnsize*10+4)+'x'+str(round(Btnsize*5.8+3))+'+18+20') # size and position
self.top.resizable(0,0)
self.top.transient(parent)
self.top.after(20, lambda: self.top.lift()) # on top
self.shiftstat = 0
X = 2
Y = 1
self.ComboBox = ttk.Combobox(self.top,height=6)
self.ComboBox.place(x=X, y=Y, width=Btnsize*10, height=round(Btnsize*0.8))
Y += round(Btnsize*0.8)
self.ComboBox.focus()
self.ComboBox['values'] = Inputvals
self.ComboBox.current(0)
self.ComboBox.icursor(END)
self.ComboBox.bind('<Return>', self.enter)
self.ComboBox.bind('<Escape>', self.escape)
self.ComboBox.bind("<<ComboboxSelected>>", self.comboselect)
self.buttons0 = [
'1','2','3','4','5','6','7','8','9','0',
'q','w','e','r','t','y','u','i','o','p',
'a','s','d','f','g','h','j','k','l','\\',
'^','z','x','c','v','b','n','m','#','<-',
'Cancel',',','Space','.','-','+','Enter']
self.buttons1 = [
'!','"','§','$','%','&','/','(',')','=',
'Q','W','E','R','T','Y','U','I','O','P',
'A','S','D','F','G','H','J','K','L','?',
'^','Z','X','C','V','B','N','M','@','<-',
'Cancel',';','Space',':','_','*','Enter']
n = 0
self.btn = list(range(len(self.buttons0)))
for label in self.buttons0:
cmd = partial(self.click, label) # or: cmd = lambda x=label:self.click(x)
if label in {'Space','Cancel','Enter'}:
self.btn[n] = Button(self.top, text=label, command=cmd, font=Font)
self.btn[n].place(x=X, y=Y, width=Btnsize*2, height=Btnsize)
X += Btnsize
else:
self.btn[n] = Button(self.top, text=label, command=cmd, font=Font)
self.btn[n].place(x=X, y=Y, width=Btnsize, height=Btnsize)
X += Btnsize
n += 1 # button index
if X > Btnsize*10:
X = 2
Y += Btnsize
self.top.focus_set() # focus
self.top.grab_set() # modal
self.result = chr(27) # ESC
def comboselect(self, event):
self.ComboBox.selection_clear()
def click(self,btn):
cursor = self.ComboBox.index(INSERT)
if btn == 'Cancel':
self.top.destroy()
elif btn == 'Enter':
self.result = self.ComboBox.get()
self.top.destroy()
elif btn == '<-':
self.ComboBox.delete(cursor-1)
elif btn == 'Space':
self.ComboBox.insert(cursor,' ')
elif (btn == '^'):
self.shift()
else:
self.ComboBox.insert(cursor,btn)
def shift(self):
n = 0
if self.shiftstat == 1:
self.shiftstat = 0
self.btn[30].config(bg='#DDDDDD')
for label in self.buttons0:
cmd = partial(self.click, label)
self.btn[n].config(text=label, command=cmd)
n += 1
else:
self.btn[30].config(bg='#AAAAAA')
self.shiftstat = 1
for label in self.buttons1:
cmd = partial(self.click, label)
self.btn[n].config(text=label, command=cmd)
n += 1
def escape(self,event):
self.top.destroy()
def enter(self,event):
self.result = self.ComboBox.get()
self.top.destroy()
def Get_Input():
Items = ['Item1','Item2', 'Item3']
KeyInput = Keyboard(root,Items,Title='Keyboard 1', Btnsize=28, Font='Helvetica 10')
root.wait_window(KeyInput.top)
if KeyInput.result != chr(27): # ESC or cancel
print ('Keyboard return: '+KeyInput.result)
root = Tk()
Get_Input()
root.mainloop()