Coder Social home page Coder Social logo

Comments (4)

monkeycc avatar monkeycc commented on July 20, 2024 1

Thank you for your help

This code is very magical

It should be written like this

@PySimpleGUI

from pysimplegui.

jason990420 avatar jason990420 commented on July 20, 2024

GUI will be updated only when window.read() or window.refresh() called after window finalized.

All other actions just to update on the architecture of sg.Window, not on the real GUI.

In this case, to update GUI immediately you can do it like

    if event == '-Button-':
        window["hello"].update(text_color="green")
        window.refresh()
        time.sleep(0.1)
        window["hello"].update(text_color="gray")

from pysimplegui.

PySimpleGUI avatar PySimpleGUI commented on July 20, 2024

A couple of tips regarding timing inside of event loops.

If possible, avoid placing sleep calls inside the event loop. The reason for this is that if you sleep for too long, then the window will appear to be hung and you'll get a warning, etc. The window becomes unresponsive during sleeps. If you have other buttons you want to interact with, they won't function correctly.

One way to deal with this is to use a timeout on your read call.

Here is code like yours except I've made the "flash" time 1 second instead of 100ms. If you want it to be 100ms, then you can change 1000 to 100 in this line of code:

led_on, on_count, flash_duration, loop_timeout = False, 0, 1000, 20

python_0l85G5oNOQ

import PySimpleGUI as sg
layout = [
    [sg.Text(sg.SYMBOL_CIRCLE, font='Default 30', text_color='gray', key="hello")],
    [sg.Button('Button1', key='-Button-')]
    ]

window = sg.Window('Window Title', layout)

led_on, on_count, flash_duration, loop_timeout = False, 0, 1000, 20

while True:             # Event Loop
    event, values = window.read(timeout=loop_timeout)
    # print(event, values)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break

    if event == '-Button-':
        window["hello"].update(text_color="green")
        led_on = True
        on_count = 0
    if led_on:
        on_count += 1
        if on_count * loop_timeout >= flash_duration:
            led_on = False
            window["hello"].update(text_color="gray")
window.close()

from pysimplegui.

PySimpleGUI avatar PySimpleGUI commented on July 20, 2024

Ooooooooo! I like "magical"!

image

Thank you for the kind words and wow, what a compliment. I'm glad you found it helpful.

These kinds of constructs originated in my history of working in embedded systems where you can't ever "sleep". You have to keep running and find ways of doing timing on your own by using state machines.

That's exactly what I've done here... is make a state machine.

There are 2 states. One where the LED is OFF (gray), the other is ON (green). Each time through the event loop, the state machine is run.

You could write a state machine diagram for it something like this:

image

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.