Skip to content

Commit

Permalink
Merge pull request #4 from hostedposted/development
Browse files Browse the repository at this point in the history
Version 1.4.0
  • Loading branch information
hostedposted authored Jun 21, 2022
2 parents 876bedb + 89869fa commit 157b4d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
31 changes: 22 additions & 9 deletions docs/call-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,48 @@ Returns a decorator. The function passed into the decorator will get called when

When you click the button ``Hello World!`` will be printed!

### Elements.button_event(key, time_limit)
### Elements.button_event(key, time_limit, delay)

[:octicons-tag-24: 1.1.0](https://github.com/hostedposted/py-gui/tree/1.1.0) - Add an element to the frame for `time_limit` seconds after a button is clicked.

| Parameter | Latest Change | Type | Required | Default Value | Description |
| :--------- | ---------------------------------------------------------------------------- | :---------------- | :--------------- | :--------------- | :------------------------------------------------------------------------------ |
| key | [:octicons-tag-24: 1.1.0](https://github.com/hostedposted/py-gui/tree/1.1.0) | string | :material-check: | :material-close: | The button's key. |
| time_limit | [:octicons-tag-24: 1.1.0](https://github.com/hostedposted/py-gui/tree/1.1.0) | seconds (integer) | :material-check: | 10 | The amount of time the elements should be rendered after the button is pressed. |
| delay | [:octicons-tag-24: 1.4.0](https://github.com/hostedposted/py-gui/tree/1.4.0) | seconds (integer) | :material-check: | 0 | Make this function only get called after ``delay`` number of seconds. |

Returns a decorator. The function passed into the decorator will get called constantly for `time_limit` seconds.
Returns a decorator. The function passed into the decorator will get called constantly for `time_limit` seconds, after `delay` seconds.

??? example

Let's redo the button example with the `Hello World` text being rendered for 10 seconds after the button is clicked.
Let's make a button that will terminate the program after 3 seconds.

```py linenums="1" hl_lines="11 12 13"
```py linenums="1" hl_lines="11 12 13 15 16 17 19 20 21 23 24 25"
import pygui

window = pygui.Window("Hello World")

@window.frame("Hello World", width=700, height=450)
def hello_world(elements: pygui.Elements):
@elements.button("Hello World!", key="hello")
def hello_world_button():
@elements.button("Terminate the program", key="terminate")
def terminate():
pass

@elements.button_event("hello", time_limit=10)
def hello_world_button_event():
elements.text("Hello World!")
@elements.button_event("terminate", time_limit=1, delay=0)
def terminate_event():
elements.text("The program will terminate in 3 seconds!")

@elements.button_event("terminate", time_limit=1, delay=1)
def terminate_event_two():
elements.text("The program will terminate in 2 seconds!")

@elements.button_event("terminate", time_limit=1, delay=2)
def terminate_event_three():
elements.text("The program will terminate in 1 second!")

@elements.button_event("terminate", time_limit=1, delay=3)
def terminate_event_exit():
exit(0)

window.start()
```
Expand Down
6 changes: 4 additions & 2 deletions pygui/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def button_handler(func):

return button_handler

def button_event(self, key: str, time_limit: int = 10):
def button_event(self, key: str, time_limit: int = 10, delay: int = 0):
"""
After a button is clicked call the passed function every `time_limit` seconds.
Expand All @@ -217,6 +217,8 @@ def button_event(self, key: str, time_limit: int = 10):
The key of the button.
time_limit : int, optional
How long this should be called after the button's click, by default 10
delay : int, optional
How long this should be called after the button's click, by default 0
Returns
-------
Expand All @@ -225,7 +227,7 @@ def button_event(self, key: str, time_limit: int = 10):
"""

def handler(func):
if imgui.get_time() - self.state.get(key, -math.inf) < time_limit:
if delay <= imgui.get_time() - self.state.get(key, -math.inf) <= time_limit + delay:
func()

return handler
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "py-gui-tool"
version = "1.3.0"
version = "1.4.0"
description = "PyGui is an easy to use gui."
authors = ["hostedposted <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 157b4d6

Please sign in to comment.