From 180be467a412e14bd950f0af9ffd2a60d1515ddc Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 4 Dec 2018 21:29:41 -0500 Subject: [PATCH 1/4] Set focus for Button --- PySimpleGUI.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 2dc5ffdb..e2e85a2c 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1506,6 +1506,14 @@ class Button(Element): def GetText(self): return self.ButtonText + + def SetFocus(self): + try: + self.TKButton.focus_set() + except: + pass + + def __del__(self): try: self.TKButton.__del__() From 8a8134fd48466ecb9156223096194f39c9b74e4c Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 5 Dec 2018 11:26:23 -0500 Subject: [PATCH 2/4] Support for file based button images, new method Button.SetFocus() --- PySimpleGUIQt/PySimpleGUIQt.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/PySimpleGUIQt/PySimpleGUIQt.py b/PySimpleGUIQt/PySimpleGUIQt.py index 76edfdfd..0fb04820 100644 --- a/PySimpleGUIQt/PySimpleGUIQt.py +++ b/PySimpleGUIQt/PySimpleGUIQt.py @@ -1472,6 +1472,10 @@ class Button(Element): def GetText(self): return self.ButtonText + def SetFocus(self): + self.QT_QPushButton.setFocus() + + def __del__(self): super().__del__() @@ -4484,6 +4488,24 @@ def PackFormIntoFrame(window, containing_frame, toplevel_win): if element_size[1] is not None: element.QT_QPushButton.setFixedHeight(element_size[1]) + + # + # elif element.Data is not None: + # qlabel.setText('') + # ba = QtCore.QByteArray.fromRawData(element.Data) + # pixmap = QtGui.QPixmap() + # pixmap.loadFromData(ba) + # qlabel.setPixmap(pixmap) + # elif element.DataBase64: + # qlabel.setText('') + # ba = QtCore.QByteArray.fromBase64(element.DataBase64) + # pixmap = QtGui.QPixmap() + # pixmap.loadFromData(ba) + # qlabel.setPixmap(pixmap) + + if element.ImageFilename is not None: + element.QT_QPushButton.setIcon(QtGui.QPixmap(element.ImageFilename)) + element.QT_QPushButton.setIconSize(QtGui.QPixmap(element.ImageFilename).rect().size()) if element.ImageData: ba = QtCore.QByteArray.fromBase64(element.ImageData) pixmap = QtGui.QPixmap() From ddbe9332c321170b526b60bdab51648bbd61f542 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 5 Dec 2018 11:26:44 -0500 Subject: [PATCH 3/4] New Demo Program! IP Address Entry --- DemoPrograms/Demo_IP_Address_Entry.py | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DemoPrograms/Demo_IP_Address_Entry.py diff --git a/DemoPrograms/Demo_IP_Address_Entry.py b/DemoPrograms/Demo_IP_Address_Entry.py new file mode 100644 index 00000000..7b92fff8 --- /dev/null +++ b/DemoPrograms/Demo_IP_Address_Entry.py @@ -0,0 +1,43 @@ +import PySimpleGUI as sg + +''' + IP Address entry window with digit validation and auto advance + If not a digit or ., the ignored + . will advance the focus to the next entry + On the last input, once it's complete the focus moves to the OK button + Pressing spacebar with focus on OK generates an _OK_ event +''' + +# create a short-cut element so don't have to type this in over and over +InIp = lambda key: sg.Input(do_not_clear=True, size=(3, 1), key=key, pad=(0, 2)) + +layout = [[sg.Text('Your typed chars appear here:'), sg.Text('', key='_OUTPUT_')], + [InIp(0), sg.T('.'), InIp(1), sg.T('.'), InIp(2), sg.T('.'), InIp(3)], + [sg.Button('Ok', key='_OK_', bind_return_key=True), sg.Button('Exit')]] + +window = sg.Window('Window Title', return_keyboard_events=True).Layout(layout) + +while True: # Event Loop + event, values = window.Read() + print(event) + if event is None or event == 'Exit': + break + elem = window.FindElementWithFocus() + if elem is not None: + key = elem.Key + value = values[key] # get value of input field that has focus + if event == '.' and key!= '_OK_': # if a ., then advance to next field + elem.Update(value[:-1]) + value = value[:-1] + next_elem = window.Element(key+1) + next_elem.SetFocus() + elif event not in '0123456789': + elem.Update(value[:-1]) + elif len(value) > 2 and key < 3: # if 2 digits typed in, move on to next input + next_elem = window.Element(key+1) + next_elem.SetFocus() + elif len(value)> 2 and key == 3: + window.Element('_OK_').SetFocus() + print('You entered IP Address {}.{}.{}.{}'.format(*values.values())) + +window.Close() From d2f4da510524fba40cf53e0193e828e81538ca2f Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 5 Dec 2018 11:28:20 -0500 Subject: [PATCH 4/4] Make portable to Qt. Collapsed definition using Lambda --- DemoPrograms/Demo_Media_Player.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/DemoPrograms/Demo_Media_Player.py b/DemoPrograms/Demo_Media_Player.py index 4ccd84bd..3d8dee6f 100644 --- a/DemoPrograms/Demo_Media_Player.py +++ b/DemoPrograms/Demo_Media_Player.py @@ -2,6 +2,7 @@ import sys if sys.version_info[0] >= 3: import PySimpleGUI as sg + # import PySimpleGUIQt as sg # portable to QT else: import PySimpleGUI27 as sg @@ -23,31 +24,18 @@ def MediaPlayerGUI(): # A text element that will be changed to display messages in the GUI + ImageButton = lambda key:sg.Button('', button_color=(background,background), image_filename=image_restart, image_size=(50, 50), image_subsample=2, border_width=0, key=key) # define layout of the rows layout= [[sg.Text('Media File Player',size=(17,1), font=("Helvetica", 25))], [sg.Text('', size=(15, 2), font=("Helvetica", 14), key='output')], - [sg.Button('', button_color=(background,background), - image_filename=image_restart, image_size=(50, 50), image_subsample=2, border_width=0, key='Restart Song'), + [ImageButton(key='Restart Song'), sg.Text(' ' * 2), + ImageButton(key='Pause'), sg.Text(' ' * 2), - sg.Button('', button_color=(background,background), - image_filename=image_pause, image_size=(50, 50), image_subsample=2, border_width=0, key='Pause'), + ImageButton(key='Next'), sg.Text(' ' * 2), - sg.Button('', button_color=(background,background), image_filename=image_next, image_size=(50, 50), image_subsample=2, border_width=0, key='Next'), - sg.Text(' ' * 2), - sg.Text(' ' * 2), sg.Button('', button_color=(background,background), - image_filename=image_exit, image_size=(50, 50), image_subsample=2, border_width=0, key='Exit')], + sg.Text(' ' * 2),ImageButton(key='Exit')], [sg.Text('_'*20)], - [sg.Text(' '*30)], - [ - sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15)), - sg.Text(' ' * 2), - sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15)), - sg.Text(' ' * 2), - sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical', font=("Helvetica", 15))], - [sg.Text(' Bass', font=("Helvetica", 15), size=(9, 1)), - sg.Text('Treble', font=("Helvetica", 15), size=(7, 1)), - sg.Text('Volume', font=("Helvetica", 15), size=(7, 1))] ] # Open a form, note that context manager can't be used generally speaking for async forms