Major update of all demo programs to use new PEP8 bindings, etc

This commit is contained in:
PySimpleGUI 2019-10-23 16:10:03 -04:00
parent 3f7c87c562
commit 7f52778bcc
307 changed files with 19546 additions and 3297 deletions

View file

@ -2,7 +2,8 @@ import PySimpleGUI as sg
import random
# ------- Sort visualizer. Displays bar chart representing list items -------
BAR_SPACING, BAR_WIDTH, EDGE_OFFSET = 11, 10, 3
DATA_SIZE = GRAPH_SIZE = (700,500) # width, height of the graph portion
DATA_SIZE = GRAPH_SIZE = (700, 500) # width, height of the graph portion
def bubble_sort(arr):
def swap(i, j):
@ -19,30 +20,34 @@ def bubble_sort(arr):
swapped = True
yield arr
def draw_bars(graph, items): # draws all the bars for all values across screen
def draw_bars(graph, items):
# type: (sg.Graph, List)->None
for i, item in enumerate(items):
graph.draw_rectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, item),
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color='#76506d')
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0),
fill_color='#76506d')
def main():
sg.change_look_and_feel('LightGreen')
# Make list to sort
num_bars = DATA_SIZE[0]//(BAR_WIDTH+1)
list_to_sort = [DATA_SIZE[1]//num_bars*i for i in range(1,num_bars)]
list_to_sort = [DATA_SIZE[1]//num_bars*i for i in range(1, num_bars)]
random.shuffle(list_to_sort)
# define window layout
graph = sg.Graph(GRAPH_SIZE, (0,0), DATA_SIZE)
graph = sg.Graph(GRAPH_SIZE, (0, 0), DATA_SIZE)
layout = [[graph],
[sg.T('Speed Faster'), sg.Slider((0,20), orientation='h', default_value=10, key='-SPEED-'), sg.T('Slower')]]
[sg.Text('Speed Faster'), sg.Slider((0, 20), orientation='h', default_value=10, key='-SPEED-'), sg.Text('Slower')]]
window = sg.Window('Sort Demonstration', layout, finalize=True)
draw_bars(graph, list_to_sort) # draw the initial window's bars
# draw the initial window's bars
draw_bars(graph, list_to_sort)
sg.popup('Click OK to begin Bubblesort') # Wait for user to start it up
bsort = bubble_sort(list_to_sort) # get an iterator for the sort
timeout=10 # start with 10ms delays between draws
timeout = 10 # start with 10ms delays between draws
while True: # ----- The event loop -----
event, values = window.read(timeout=timeout)
if event is None:
@ -52,8 +57,11 @@ def main():
except:
sg.popup('Sorting done!')
break
graph.Erase()
graph.erase()
draw_bars(graph, partially_sorted_list)
timeout = int(values['-SPEED-'])
window.close()
main()
main()