New demo keypad, New Window method - FindElementWithFocus
This commit is contained in:
parent
523231141a
commit
23027173a5
|
@ -0,0 +1,91 @@
|
||||||
|
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()
|
||||||
|
if focus is not None:
|
||||||
|
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:
|
||||||
|
cur_focus = self.mainWindow.FindElementWithFocus()
|
||||||
|
if cur_focus is not None:
|
||||||
|
self.focus = cur_focus
|
||||||
|
event, values = self.mainWindow.ReadNonBlocking()
|
||||||
|
if self.focus is not None:
|
||||||
|
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()
|
|
@ -2683,15 +2683,17 @@ class Window:
|
||||||
element = _FindElementFromKeyInSubForm(self, key)
|
element = _FindElementFromKeyInSubForm(self, key)
|
||||||
if element is None:
|
if element is None:
|
||||||
print('*** WARNING = FindElement did not find the key. Please check your key\'s spelling ***')
|
print('*** WARNING = FindElement did not find the key. Please check your key\'s spelling ***')
|
||||||
if element is None:
|
PopupError('Keyword error in FindElement Call',
|
||||||
print('*** WARNING = FindElement did not find the key. Please check your key\'s spelling ***')
|
'Bad key = {}'.format(key),
|
||||||
PopupError('Keyword error in FindElement Call',
|
'Your bad line of code may resemble this:',
|
||||||
'Bad key = {}'.format(key),
|
'window.FindElement("{}")'.format(key))
|
||||||
'Your bad line of code may resemble this:',
|
|
||||||
'window.FindElement("{}")'.format(key))
|
|
||||||
return ErrorElement(key=key)
|
return ErrorElement(key=key)
|
||||||
return element
|
return element
|
||||||
|
|
||||||
|
def FindElementWithFocus(self):
|
||||||
|
element = _FindElementWithFocusInSubForm(self)
|
||||||
|
return element
|
||||||
|
|
||||||
def SaveToDisk(self, filename):
|
def SaveToDisk(self, filename):
|
||||||
try:
|
try:
|
||||||
results = BuildResults(self, False, self)
|
results = BuildResults(self, False, self)
|
||||||
|
@ -3228,6 +3230,29 @@ def _FindElementFromKeyInSubForm(form, key):
|
||||||
return element
|
return element
|
||||||
|
|
||||||
|
|
||||||
|
def _FindElementWithFocusInSubForm(form):
|
||||||
|
for row_num, row in enumerate(form.Rows):
|
||||||
|
for col_num, element in enumerate(row):
|
||||||
|
if element.Type == ELEM_TYPE_COLUMN:
|
||||||
|
matching_elem = _FindElementWithFocusInSubForm(element)
|
||||||
|
if matching_elem is not None:
|
||||||
|
return matching_elem
|
||||||
|
if element.Type == ELEM_TYPE_FRAME:
|
||||||
|
matching_elem = _FindElementWithFocusInSubForm(element)
|
||||||
|
if matching_elem is not None:
|
||||||
|
return matching_elem
|
||||||
|
if element.Type == ELEM_TYPE_TAB_GROUP:
|
||||||
|
matching_elem = _FindElementWithFocusInSubForm(element)
|
||||||
|
if matching_elem is not None:
|
||||||
|
return matching_elem
|
||||||
|
if element.Type == ELEM_TYPE_TAB:
|
||||||
|
matching_elem = _FindElementWithFocusInSubForm(element)
|
||||||
|
if matching_elem is not None:
|
||||||
|
return matching_elem
|
||||||
|
if element.Type == ELEM_TYPE_INPUT_TEXT:
|
||||||
|
if element.TKEntry is not None:
|
||||||
|
if element.TKEntry is element.TKEntry.focus_get():
|
||||||
|
return element
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
def AddMenuItem(top_menu, sub_menu_info, element, is_sub_menu=False, skip=False):
|
def AddMenuItem(top_menu, sub_menu_info, element, is_sub_menu=False, skip=False):
|
||||||
|
|
Loading…
Reference in New Issue