From f17f7722b730002a70045bf0dc90b063403d29ff Mon Sep 17 00:00:00 2001 From: jackyOO7 <44204857+jackyOO7@users.noreply.github.com> Date: Fri, 19 Oct 2018 15:04:21 +0200 Subject: [PATCH] Create Demo_Touch_Keyboard I needed a simple keyboard to use the GUI on a touchscreen system without keyboard. Turned out to be quite tricky and I do not think that this is the best way to do it. But in principle it works. I didn't manage to get it working with Multilines and Comboboxes though. Maybe someone with better understanding of PySimpleGUI and tkinter can offer a more universal and elegant way... --- Demo_Touch_Keyboard | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Demo_Touch_Keyboard diff --git a/Demo_Touch_Keyboard b/Demo_Touch_Keyboard new file mode 100644 index 00000000..ac8eebed --- /dev/null +++ b/Demo_Touch_Keyboard @@ -0,0 +1,84 @@ +import PySimpleGUI as sg + +class keyboard(): + def __init__(self,font=('Arial',16)): + self.font=font + numberRow='1234567890' + topRow='QWERTYUIOP' + midRow='ASDFGHJKL' + bottomRow='ZXCVBNM' + keyboard_layout=[[sg.ReadButton(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in numberRow]+[sg.ReadButton('⌫', key='back', pad=(0, 0), size=(4, 2), font=self.font),sg.ReadButton('Esc', key='close', pad=(0, 0), size=(4, 2), font=self.font)], + [sg.T(' '*4)]+[sg.ReadButton(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in topRow], + [sg.T(' '*11)]+[sg.ReadButton(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in midRow], + [sg.T(' '*18)]+[sg.ReadButton(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in bottomRow]] + + self.window=sg.Window('keyboard', grab_anywhere=True, keep_on_top=True, no_titlebar=True).Layout( + keyboard_layout).Finalize() + self.hide() + + def _keyboardhandler(self): + if self.event is not None: + if self.event=='close': + self.hide() + elif len(self.event)==1: + self.focus.Update(self.focus.Get()+self.event) + elif self.event=='back': + Text=self.focus.Get() + if len(Text)>0: + Text=Text[:-1] + self.focus.Update(Text) + + def hide(self): + self.visible=False + self.window.Disappear() + + def show(self): + self.visible=True + self.window.Reappear() + + def togglevis(self): + if self.visible: + self.hide() + else: + self.show() + + def update(self,focus): + self.event,_=self.window.ReadNonBlocking() + self.focus=focus + self._keyboardhandler() + + def close(self): + self.window.CloseNonBlocking() + + +class GUI(): + def __init__(self): + layout = [[sg.Text('Enter Text')], + [sg.Input(size=(17, 1), key='input1',)], + [sg.InputText(size=(17, 1), key='input2')], + [sg.ReadButton('on-screen keyboard',key='keyboard')], + [sg.ReadButton('close',key='close')]] + + self.mainWindow = sg.Window('On-screen test', grab_anywhere=False,no_titlebar=True).Layout(layout) + self.keyboard=keyboard() + self.focus=None + + + def run(self): + while True: + for row in self.mainWindow.Rows: + for element in row: + if element.Type=='input' and element.TKEntry is not None and element.TKEntry is element.TKEntry.focus_get(): + self.focus=element + event, values=self.mainWindow.ReadNonBlocking() + self.keyboard.update(self.focus) + if event=='keyboard': + self.keyboard.togglevis() + elif event=='close': + break + self.keyboard.close() + self.mainWindow.CloseNonBlocking() + +if __name__ == '__main__': + app=GUI() + app.run()