Added a "Pause/Resume" button.

This commit is contained in:
PySimpleGUI 2020-10-13 12:03:25 -04:00
parent 930747a324
commit 28eafd6a1a
1 changed files with 19 additions and 6 deletions

View File

@ -1,7 +1,6 @@
import PySimpleGUI as sg import PySimpleGUI as sg
import pymunk import pymunk
import random import random
""" """
Demo of pymunk physics lib combined with a large Window that is transparent. Result appears like Demo of pymunk physics lib combined with a large Window that is transparent. Result appears like
a screensaver type of screen a screensaver type of screen
@ -57,30 +56,44 @@ def main():
graph_elem = sg.Graph(screensize, (0, screensize[1]), (screensize[0], 0), graph_elem = sg.Graph(screensize, (0, screensize[1]), (screensize[0], 0),
enable_events=True, key='-GRAPH-', background_color='lightblue') enable_events=True, key='-GRAPH-', background_color='lightblue')
layout = [[graph_elem]] layout = [[graph_elem]]
window1 = sg.Window('Bouncing Balls', layout, finalize=True, location=(0,0), keep_on_top=True, element_padding=(0,0), margins=(0,0), no_titlebar=True, transparent_color='lightblue') window1 = sg.Window('Bouncing Balls', layout, finalize=True, location=(0,0), keep_on_top=True, element_padding=(0,0), margins=(0,0), no_titlebar=True, right_click_menu=[[''], ['Front', 'Back', 'Controls', 'Exit']])
area = Playfield(graph_elem, screensize) area = Playfield(graph_elem, screensize)
area.add_balls() area.add_balls()
transparent, paused = False, True
layout2 = [[sg.B('', border_width=0, button_color=('white', sg.theme_background_color()), key='Exit')],[sg.B('Kick'), sg.B('Front'), sg.B('Back'), sg.B('More Balls'),]] layout2 = [[sg.B('', border_width=0, button_color=('white', sg.theme_background_color()), key='Exit')],[sg.B('Kick'), sg.B('Front'), sg.B('Back'), sg.B('More Balls'),sg.B('Transparent'), sg.B('Resume', key='Pause')]]
window2 = sg.Window('Buttons', layout2, keep_on_top=True, grab_anywhere=True, no_titlebar=True, finalize=True) window2 = sg.Window('Buttons', layout2, keep_on_top=True, grab_anywhere=True, no_titlebar=True, finalize=True)
# ------------------- GUI Event Loop ------------------- # ------------------- GUI Event Loop -------------------
while True: while True:
window, event, values = sg.read_all_windows(timeout=0) window, event, values = sg.read_all_windows(timeout=0)
if event in (sg.WIN_CLOSED, 'Exit'): if event in (sg.WIN_CLOSED, 'Exit'):
break break
area.space.step(0.02)
if event == 'More Balls': if event == 'More Balls':
area.add_balls() area.add_balls()
elif event == 'Kick': elif event == 'Kick':
for ball in area.arena_balls: for ball in area.arena_balls:
ball.body.position = ball.body.position[0], ball.body.position[1]-random.randint(1, 200) ball.body.position = ball.body.position[0], ball.body.position[1]-random.randint(200,400)
elif event == 'Front': elif event == 'Front':
window1.bring_to_front() window1.bring_to_front()
elif event == 'Back': elif event == 'Back':
window1.send_to_back() window1.send_to_back()
elif event == 'Transparent':
window1.set_transparent_color('lightblue' if not transparent else 'black')
transparent = not transparent
elif event == 'Controls':
window2.bring_to_front()
elif event == 'Pause':
paused = not paused
window['Pause'].update(text='Resume' if paused else 'Pause')
if paused:
continue
area.space.step(0.02)
for ball in area.arena_balls: for ball in area.arena_balls:
if ball.body.position[1] > screensize[1]: if ball.body.position[1] > screensize[1]: