From 15b4b9701b6a8c1cb3203ca8a59b37fc05e15cf0 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 19 Oct 2018 13:57:25 -0400 Subject: [PATCH] Changes submitted by other users... good stuff! --- Demo_OpenCV_Webcam.py | 23 +++--------- Demo_Touch_Keyboard.py | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 Demo_Touch_Keyboard.py diff --git a/Demo_OpenCV_Webcam.py b/Demo_OpenCV_Webcam.py index c21eaea0..c6fb89c3 100644 --- a/Demo_OpenCV_Webcam.py +++ b/Demo_OpenCV_Webcam.py @@ -4,10 +4,8 @@ if sys.version_info[0] >= 3: import PySimpleGUI as sg else: import PySimpleGUI27 as sg -import cv2 as cv -from PIL import Image +import cv2 import numpy as np -import io from sys import exit as exit """ @@ -31,7 +29,7 @@ def main(): window.Layout(layout).Finalize() # ---===--- Event LOOP Read and display frames, operate the GUI --- # - cap = cv.VideoCapture(0) + cap = cv2.VideoCapture(0) recording = False while True: event, values = window.ReadNonBlocking() @@ -42,10 +40,8 @@ def main(): recording = True elif event == 'Stop': recording = False - img = Image.new('RGB', (640, 480), (255, 255, 255)) - bio = io.BytesIO() # a binary memory resident stream - img.save(bio, format='PNG') # save image as png to it - imgbytes = bio.getvalue() + img = np.full((480, 640),255) + imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes window.FindElement('image').Update(data=imgbytes) elif event == 'About': sg.PopupNoWait('Made with PySimpleGUI', @@ -56,14 +52,7 @@ def main(): keep_on_top=True) if recording: ret, frame = cap.read() - - gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) - - # let img be the PIL image - img = Image.fromarray(gray) # create PIL image from frame - bio = io.BytesIO() # a binary memory resident stream - img.save(bio, format= 'PNG') # save image as png to it - imgbytes = bio.getvalue() # this can be used by OpenCV hopefully + imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto window.FindElement('image').Update(data=imgbytes) -main() \ No newline at end of file +main() diff --git a/Demo_Touch_Keyboard.py b/Demo_Touch_Keyboard.py new file mode 100644 index 00000000..ac8eebed --- /dev/null +++ b/Demo_Touch_Keyboard.py @@ -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()