Meinen Workarount habe ich fertiggestellt und getestet. Der umfasst zwar kein komplettes Keyboard mit allen Tasten aber für meine Zwecke absolut ausreichend.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from tkinter import Button, Frame, LabelFrame, Label, Entry, Tk, font, INSERT, END
from functools import partial
from collections import deque
class GUI(Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Keyboard-Tester")
self.grid_columnconfigure(0,weight=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(1, weight=1)
self.keyboard_font = font.Font(family="Lucida Grande", size=20)
self.form=Form(self)
self.form.grid(row=0,column=0,padx=5,pady=5,sticky="new")
self.keyboard=Keyboard(self)
self.keyboard.grid(row=1,column=0,padx=5,pady=5,sticky="nesw")
self.entries = deque([self.form.eingabe1,self.form.eingabe2,self.form.eingabe3])
self.entries[0].focus()
class Form(LabelFrame):
def __init__(self, parent, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parent=parent
self.border=1
self.textfeld=""
self.label1=Label(self,text="Eingabefeld 1",font=self.parent.keyboard_font)
self.label1.grid(row=0,column=0,padx=5,pady=10)
self.eingabe1=Entry(self,width=80,font=self.parent.keyboard_font)
self.eingabe1.grid(row=0,column=1,padx=5,pady=10)
self.label2=Label(self,text="Eingabefeld 2",font=self.parent.keyboard_font)
self.label2.grid(row=1,column=0,padx=5,pady=10)
self.eingabe2=Entry(self,width=80,font=self.parent.keyboard_font)
self.eingabe2.grid(row=1,column=1,padx=5,pady=10)
self.label3=Label(self,text="Eingabefeld 3",font=self.parent.keyboard_font)
self.label3.grid(row=2,column=0,padx=5,pady=10)
self.eingabe3=Entry(self,width=80,font=self.parent.keyboard_font)
self.eingabe3.grid(row=2,column=1,padx=5,pady=10)
self.eingabe1.focus()
class Keyboard(Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parent=parent
self.shifted=False
self.locked2upper=False
self.grid_setup()
self.button_setup()
def function_key_pressed(self,caption,position):
focused_widget = self.parent.form.focus_get()
if position == 0:
if isinstance(focused_widget, Entry):
cp = focused_widget.index(INSERT)
if cp > 0:
focused_widget.delete(cp-1,cp)
elif position == 1:
self.parent.entries.rotate(-1)
self.parent.entries[0].focus()
elif position == 2:
if isinstance(focused_widget, Entry):
cp = focused_widget.index(INSERT)
if cp > 0:
focused_widget.delete(0,END)
elif position == 3:
if self.locked2upper:
self.locked2upper = False
self.shifted = False
else:
self.locked2upper = True
self.shifted = True
self.caption_list_setup()
self.key_setup()
elif position == 4:
self.shift_func()
elif position == 5:
self.shift_func()
else:
self.key_pressed(" ")
def shift_func(self):
if not self.locked2upper:
if self.shifted:
self.shifted = False
else:
self.shifted = True
self.caption_list_setup()
self.key_setup()
def key_pressed(self,key):
focused_widget = self.parent.form.focus_get()
if isinstance(focused_widget, Entry):
cp = focused_widget.index(INSERT)
focused_widget.insert(cp,str(key))
if not self.locked2upper:
if self.shifted:
self.shifted = False
self.caption_list_setup()
self.key_setup()
def grid_setup(self):
for i in range(0,15):
self.grid_columnconfigure(i,weight=1)
for i in range(0,5):
self.grid_rowconfigure(i, weight=1)
def button_setup(self):
self.caption_list_setup()
self.functional_buttons()
self.key_setup()
def caption_list_setup(self):
self.function_list = [
["Backspace",0,13,1,2],
["Tab",1,0,1,2],
["Enter",1,14,2,1],
["Lock",2,0,1,2],
["Shift",3,0,1,2],
["Shift",3,13,1,2],
["Space",4,4,1,7]
]
if not self.shifted:
self.key_list = [
[0,["~","1","2","3","4","5","6","7","8","9","0","ß","´"]],
[2,["q","w","e","r","t","z","u","i","o","p","ü","+"]],
[2,["a","s","d","f","g","h","j","k","l","ö","ä","#"]],
[2,["<","y","x","c","v","b","n","m",",",".","-"]]
]
else:
self.key_list = [
[0,["°","!",'"',"§","$","%","&","/","(",")","=","?","`"]],
[2,["Q","W","E","R","T","Z","U","I","O","P","Ü","*"]],
[2,["A","S","D","F","G","H","J","K","L","Ö","Ä","'"]],
[2,[">","Y","X","C","V","B","N","M",";",":","_"]],
]
def functional_buttons(self):
for position, caption in enumerate(self.function_list):
Button(
self,
text=caption[0],
command=partial(
self.function_key_pressed,
caption[0],
position
),
font=self.parent.keyboard_font
).grid(
row=caption[1],
column=caption[2],
rowspan=caption[3],
columnspan=caption[4],
pady=5,
padx=5,
sticky="nesw"
)
def key_setup(self):
for row,val in enumerate(self.key_list):
offset = val[0]
for col,caption in enumerate(val[1]):
column = col+offset
Button(
self,
text=caption,command=partial(
self.key_pressed,
caption
),
font=self.parent.keyboard_font
).grid(
row=row,
column=column,
pady=5,
padx=5,
sticky="nesw"
)
def main():
try:
app = GUI()
app.mainloop()
except KeyboardInterrupt:
print("Abbruch durch Benutzer")
app.destroy()
finally:
print("bis dann...")
if __name__ == "__main__":
main()
Display More
Mein nächster Schritt damit wird das Einbinden der Keyboard Klasse in meinen Geldokumenter sein.
Sobald eines der Eingabefelder den Fokus erhält wird das Keyboard sichtbar und kann zur Eingabe genutzt werden.
Hierzu verstecke ich einen Frame der GUI und zeige den Keyboard-Frame. Für den Normalbetrieb (ohne Keyboard) wird einfach der versteckte Frame wieder angezeigt und der Keyboard-Frame versteckt.