Comments (4)
Thank you for your help
This code is very magical
It should be written like this
from pysimplegui.
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.
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
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.
Ooooooooo! I like "magical"!
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:
from pysimplegui.
Related Issues (20)
- [ Bug] Garbled output on WSL HOT 5
- Question. How to get a value from a return? HOT 3
- [Question] PySimpleGUI Web Deprecated? HOT 1
- Bug: Error dialog when programatically updating PySimpleGUIQt Checkbox HOT 5
- [Question] How to add space between the background and text in input HOT 5
- [Question] I am unable to center frames inside column HOT 4
- [ Question] Window size is not restored properly when using C++ Window methods. HOT 1
- [Question] ImageFont OSError: cannot open resource HOT 1
- [Bug] Column doesn't expand when placed in a column with scrollable = True HOT 7
- [Question] How do I change the on hover color of scroll bar? HOT 3
- [Question] PySide cannot accept a list of objects as the values for the Combobox element HOT 1
- [Question] PSG with Python 3.12.4 HOT 3
- [ Enhancement] Option to create window in maximized state HOT 4
- [Bug] PSG5 UnicodeDecodeError When Freezing with cx_Freeze HOT 21
- [Enhancement] Udemy Course Enhancement Request: Context Menu Course Module HOT 2
- Error HOT 13
- [Bug] Custom Titlebar window focus problems HOT 2
- [Question] scrolling 2 tables HOT 23
- [Question] Showing "Trial period ends...." HOT 1
- [Bug] Element still visible after setting visible=False HOT 3
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.