ecookbook restoring the folders for testing...

This commit is contained in:
PySimpleGUI 2022-06-30 15:54:57 -04:00
parent 0574702876
commit 9f40a47d30
109 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,41 @@
## Desektop Widget - Launcher Bar
### Trivial Operations - Changing layouts
This demo has a nice little "minimize" feature. You click on a downarrow and the entire interface will "minimize" down into a single image.
Here is the code that does those minimize and restore operations:
```python
elif event == sg.SYMBOL_DOWN_ARROWHEAD:
window['-BUTTON COL-'].update(visible=False)
window['-MINIMIZED COL-'].update(visible=True)
elif event == '-MINIMIZED IMAGE-':
window['-BUTTON COL-'].update(visible=True)
window['-MINIMIZED COL-'].update(visible=False)
```
If you're minimizing, then you want to hide the buttons and show the image. If you're restoring, then you're hiding the image and showing the panel of buttons.
A simple thing to describe means less code for you and less complexity too.
### The Button Bar
Add a floating bar that enables easy launching of all programs on your system. Or launch your Python program or anything you want because you're a Python programmer.
This is a copy of the Demo Program you'll find on the PySimpleGUI Repo:
https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Desktop_Widget_Launcher_Bar.py
It's a simple "launcher" application that you can run, move anywhere you want, and it'll return there next time you run it.
Add your own buttons, images, etc, to launch your favorite programs, Python code, or call functions. Anything's possible when you write your own utilities
![thumb_112.png](/api/files/61574bc56525133c451d4a2a/thumb_112.png "thumb_112.png")
For this Trinket, I matched the background color and used the settings feature to set it. This is why you will see a JSON file with the Trinket.
<iframe src='https://trinket.io/embed/pygame/2593918099?start=result' width='100%' height='500' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>

View file

@ -0,0 +1,8 @@
## Using a Thread to complete a long task
The demo shows you how to use a thread to perform an operation that takes a long time to complete. Normally if you attempt to do these operations in your GUI code, it will cause your GUI to appear to have stopped.
This examples relies on global variables for the handshake between the GUI and the worker thread. Other demos you'll find here use Queues to communicate. This demo is meant to be very simple so that you do not need to use a Queue.
<iframe src='https://trinket.io/embed/pygame/a656aafde4?start=result' width='100%' height='500' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>

View file

@ -0,0 +1,10 @@
### Using a Thread and a Queue To Accomplish Long Tasks
Queues are a great way for threads to communicate. In this demo program we have a single long-operation that needs to be run. You'll find a function named `long_function_wrapper` where is where you will place your code that takes a long time to execute.
When this function completes running, it sends a message to a Queue which is monitored by the main GUI thread.
The GUI enables you to run more than 1 of these long-running tasks if you want. It keeps track of how many have been requested and tracks when each completes executing.
<iframe src='https://trinket.io/embed/pygame/0a48f0afe8?start=result' width='100%' height='500' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>

View file

@ -0,0 +1,8 @@
### Multiple Background Threads Outputting Results in GUI
Sometimes you have situations where you have persistent threads that run constantly in the background. These threads may need to output information to your GUI. Becuase you cannot directly call PySimpleGUI (tkinter) from another Thread, a communication mechanism is needed for the threads to communicate with the main GUI thread.
This communication is often in the form of a queue. This demo program runs 3 threads. Each update their status in the GUI by sending a message to a queue that the GUI is monitoring.
<iframe src='https://trinket.io/embed/pygame/a92c0346e2?start=result' width='100%' height='500' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>

View file

@ -0,0 +1,39 @@
## Example using `Window.write_event_value`
In July 2020 an important upgrade was added to the multithreaded capabilities of PySimpleGUI. Gone are the requirements to poll for incoming messages from threads. Now "events" from threads are received through the normal event loop.
The technique has several benefits including:
1. Efficiency - Polling is inefficient. Removing the polling added back a lot of CPI time
2. Simplicity - Communicating between a window and a thread is a single function call, `Window.write_event_value`
3. Encapsulation - The actual communication between the thread and the user's application is encapsulated within PySimpleGUI itself using a `queue.Queue` object.
This specific demo shows a couple of newer features in addition to the `write_event_value` call. It also shows how the routing of `cprint` calls can be accomplished by the `Multiline` definition itself.
### Communicating Between Thread and Event Loop
The most important line of code in this entire program is this one:
```python
window.write_event_value('-THREAD-', (threading.current_thread().name, i))
```
This call will cause an event to be generated and is read in the event loop when the main program calls `window.read()`.
In this example, the event that will be generated is `'-THREAD-'`
The values dictionary will also have an entry associated with that "key". In this call we passed a tuple with the values:
```python
(threading.current_thread().name, i)
```
The first entry in the tuple is the name that Python assigned the thread. The second part of the tuple is a counter. You will see these values in the output window in red.
### Screenshot on Windows
On windows, the output looks something like this:
![SNAG-0865.jpg](/api/files/5f133f9986e5654c74274796/snag-0865.jpeg "SNAG-0865.jpg")
<iframe src='https://trinket.io/embed/pygame/f1e7022af2?start=result' width='100%' height='600' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>

View file

@ -0,0 +1,37 @@
## Easing into threading
I'm assuming you're on this page because you've learned something new.... that calling a lengthy function from a GUI is not a straightforward operation if you want a good user experience.
At some point in your GUI programming you're likely to run into this common, but distressing, problem of dealing with lengthy operations in a way that feels good to the user.
The PySimpleGUI Cookbook doing into this in much more detail in this section about multi-threading:
https://pysimplegui.readthedocs.io/en/latest/cookbook/#recipe-long-operations-multi-threading
### Threading with some help...
To get you over the initial hump of multi-threaded programming, you can let PySimpleGUI create and manage threads for you. Like other APIs in PySimpleGUI, it's been simplified down as far as possible.
Here's the basic steps using `perform_long_operation`:
1. Pass your function name and a key to the call to `window.perform_long_operation`
2. Continue running your GUI event loop
3. Windows pend using their typical `window.read()` call
4. You will get the event when your function returns
5. The values dictionary will contain your function's return value. They key will be the same as the event. So, `values[event]` is your function's return value.
### Detailed Example
In this example, your long operation takes a full 15 seconds.... an eternity when you're waiting with a GUI that is not operating during that timeframe.
In this Trinket you're given 2 basic ways of performing your long operations:
1. "Go" Button - This will directly call your function
2. "Threaded" Button - This will use a thread to make the call
Use the "Dummy" button to generate events to see if a window is responding to your clicks and typing. Try moving the window as well. You won't be able to when directly calling the function here on Trinket. You'll be able to on Windows, Linux, Mac, but not here on Trinket because of how titlebars are implemented. The details aren't important. What's important is that your window is not happy when you directly call your function.
<iframe src='https://trinket.io/embed/pygame/9b3a04320d?start=result' width='100%' height='500' frameborder='0' marginwidth='0' marginheight='0' allowfullscreen></iframe>