Changes submitted by other users... good stuff!

This commit is contained in:
MikeTheWatchGuy 2018-10-19 13:57:25 -04:00
parent 495bdb5eec
commit 15b4b9701b
2 changed files with 90 additions and 17 deletions

View File

@ -4,10 +4,8 @@ if sys.version_info[0] >= 3:
import PySimpleGUI as sg import PySimpleGUI as sg
else: else:
import PySimpleGUI27 as sg import PySimpleGUI27 as sg
import cv2 as cv import cv2
from PIL import Image
import numpy as np import numpy as np
import io
from sys import exit as exit from sys import exit as exit
""" """
@ -31,7 +29,7 @@ def main():
window.Layout(layout).Finalize() window.Layout(layout).Finalize()
# ---===--- Event LOOP Read and display frames, operate the GUI --- # # ---===--- Event LOOP Read and display frames, operate the GUI --- #
cap = cv.VideoCapture(0) cap = cv2.VideoCapture(0)
recording = False recording = False
while True: while True:
event, values = window.ReadNonBlocking() event, values = window.ReadNonBlocking()
@ -42,10 +40,8 @@ def main():
recording = True recording = True
elif event == 'Stop': elif event == 'Stop':
recording = False recording = False
img = Image.new('RGB', (640, 480), (255, 255, 255)) img = np.full((480, 640),255)
bio = io.BytesIO() # a binary memory resident stream imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes
img.save(bio, format='PNG') # save image as png to it
imgbytes = bio.getvalue()
window.FindElement('image').Update(data=imgbytes) window.FindElement('image').Update(data=imgbytes)
elif event == 'About': elif event == 'About':
sg.PopupNoWait('Made with PySimpleGUI', sg.PopupNoWait('Made with PySimpleGUI',
@ -56,14 +52,7 @@ def main():
keep_on_top=True) keep_on_top=True)
if recording: if recording:
ret, frame = cap.read() ret, frame = cap.read()
imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto
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
window.FindElement('image').Update(data=imgbytes) window.FindElement('image').Update(data=imgbytes)
main() main()

84
Demo_Touch_Keyboard.py Normal file
View File

@ -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()