Giter VIP home page Giter VIP logo

Comments (4)

bloodsuckant avatar bloodsuckant commented on May 29, 2024 1

Thank you!

from pysimplegui.

jason990420 avatar jason990420 commented on May 29, 2024

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.

PySimpleGUI avatar PySimpleGUI commented on May 29, 2024

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.

image

from pysimplegui.

PySimpleGUI avatar PySimpleGUI commented on May 29, 2024

The eCookbook also has a few multithreaded examples.

https://docs.pysimplegui.com/en/latest/cookbook/ecookbook/advanced/multi-threaded/window-perform_long_operation-method/

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.