Comments (4)
Thank you!
from pysimplegui.
Please refer the section for Multi-threading
Multi-threading
IMPORTANT!
The first thing to understand with threading in PySimpleGUI (the tkinter port in particular) is that you must not make any calls into PySimpleGUI from a thread that are GUI related... with one exception: window.write_event_value.
Revised code,
import threading
import cv2
import PySimpleGUI as sg
def RecordFun():
global recording
for i in range(1000):
if recording:
ret, frame = cap.read()
imgbytes = cv2.imencode('.png', frame)[1].tobytes() # ditto
window.write_event_value("Update", imgbytes)
else:
break
window.write_event_value("Done", None)
sg.theme('Black')
# define the window layout
layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')],
[sg.Image(filename='', key='image')],
[sg.Button('Record', size=(10, 1), font='Helvetica 14'),
sg.Button('Stop', size=(10, 1), font='Helvetica 14'),
sg.Button('Exit', size=(10, 1), font='Helvetica 14'), ]]
# create the window and show it without the plot
window = sg.Window('Demo Application - OpenCV Integration',
layout, location=(800, 400), enable_close_attempted_event=True)
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
cap = cv2.VideoCapture(0)
recording, ending = False, False
while True:
event, values = window.read(timeout=20)
if event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT or event == 'Exit':
if not recording:
break
recording = False
ending = True
elif event == 'Record' and not recording:
window['Record'].update(disabled=True)
recording = True
thread_IMloop = threading.Thread(target=RecordFun, daemon=True)
thread_IMloop.start()
elif event == "Stop":
recording = False
window['Record'].update(disabled=False)
elif event == "Update":
imgbytes = values[event]
window['image'].update(data=imgbytes)
elif event == "Done":
window['Record'].update(disabled=False)
if ending:
break
window.close()
cap.release()
from pysimplegui.
Take a look at the Demo Programs. You'll find a number of them use threads and the write_event_value
call. They're a good starting point. There are also some in the eCookbook that you can run in your browser.
from pysimplegui.
The eCookbook also has a few multithreaded examples.
You can use the threading module or there are some threading methods built into PySimpleGUI. Regardless of how you start your threads, the only call a thread can make is window.write_event_value
.
from pysimplegui.
Related Issues (20)
- [Bug] Event handling is broken on the macOS Sonoma 14.2.1 with tkinter backend HOT 1
- [ Bug] The col_widths parameter of PySimpleGUI.Table does not take effect. HOT 3
- [Question] How do I redraw the entire window layout in PySimpleGUI HOT 4
- [Question] Is there a way to blur the rectangle that contains a Matplotlib plot? HOT 9
- [ Bug ] Problem with HID device at first fire in the edit focused in pysimplegui tkinter. HOT 8
- [Question] Is there a better way to convert a JPG image into a PNG? HOT 5
- [ Question] Is it possible for me to force update the values dictionary for my menubutton elements to None? HOT 10
- [ Enhancement/Question] Programmatically setting Horizontal Scroll position of a Column() HOT 9
- [Question] Demo_Matplotlib_Ping_Graph_Large.py does not run/compile on Pi4 HOT 26
- Flask click SystemError when used with pySimpleGUI output redirect HOT 1
- [Question] How to trap an event HOT 2
- [Enhancement] CI friendly handling for the first time installation dialog HOT 3
- [Question] How to update class parameter within inherited class HOT 4
- [Question] How To Re-Enter Data For A Specific Field - AND 'CR to act like TAB HOT 1
- [ Bug] sg.ColorChooserButton Unless Additional Tkinter Import HOT 1
- [Bug] Hitting the image-count limit [Fail to create pixmap with Tk_GetPixmap in TkImgPhotoInstanceSetSize] HOT 6
- [Question] Import error HOT 3
- [Question] Change focus highlight when tabbing to button HOT 8
- [ Enhancement/Bug/Question] NOTE - you can also call sg.main() or sg.main_open_github_issue() to post an issue HOT 1
- [ Question] Compiling Errors since License validation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pysimplegui.