2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
2018-09-24 22:01:00 +00:00
import sys
2018-10-15 20:07:23 +00:00
2018-09-28 18:57:37 +00:00
if sys . version_info [ 0 ] > = 3 :
2018-09-27 20:24:09 +00:00
import PySimpleGUI as sg
2018-09-28 18:57:37 +00:00
else :
import PySimpleGUI27 as sg
2019-03-19 15:53:03 +00:00
import random
import string
2019-07-20 02:22:49 +00:00
2019-03-19 15:53:03 +00:00
# ------------------ Create a fake table ------------------
class Fake ( ) :
@classmethod
def word ( self ) :
return ' ' . join ( random . choice ( string . ascii_lowercase ) for i in range ( 10 ) )
@classmethod
def number ( self , max = 1000 ) :
return random . randint ( 0 , max )
2018-09-13 16:13:27 +00:00
2019-03-19 15:53:03 +00:00
def make_table ( num_rows , num_cols ) :
data = [ [ j for j in range ( num_cols ) ] for i in range ( num_rows ) ]
data [ 0 ] = [ Fake . word ( ) for _ in range ( num_cols ) ]
for i in range ( 1 , num_rows ) :
data [ i ] = [ Fake . word ( ) , * [ Fake . number ( ) for i in range ( num_cols - 1 ) ] ]
return data
data = make_table ( num_rows = 15 , num_cols = 6 )
2018-10-29 00:01:03 +00:00
# sg.SetOptions(element_padding=(0,0))
2018-10-15 20:07:23 +00:00
headings = [ data [ 0 ] [ x ] for x in range ( len ( data [ 0 ] ) ) ]
2019-07-20 02:22:49 +00:00
layout = [ [ sg . Table ( values = data [ 1 : ] [ : ] , headings = headings , max_col_width = 25 , background_color = ' lightblue ' ,
auto_size_columns = True , display_row_numbers = True , justification = ' right ' , num_rows = 20 , alternating_row_color = ' blue ' , key = ' _table_ ' , tooltip = ' This is a table ' ) ] ,
[ sg . Button ( ' Read ' ) , sg . Button ( ' Double ' ) , sg . Button ( ' Update ' ) ] ,
2018-10-29 00:01:03 +00:00
[ sg . T ( ' Read = read which rows are selected ' ) ] , [ sg . T ( ' Double = double the amount of data in the table ' ) ] ]
window = sg . Window ( ' Table ' , grab_anywhere = False , resizable = True ) . Layout ( layout )
while True :
event , values = window . Read ( )
2019-07-20 02:22:49 +00:00
print ( event , values )
2018-10-29 00:01:03 +00:00
if event is None :
break
if event == ' Double ' :
for i in range ( len ( data ) ) :
data . append ( data [ i ] )
window . FindElement ( ' _table_ ' ) . Update ( values = data )
2019-07-20 02:22:49 +00:00
elif event == ' Update ' :
2019-07-20 02:32:46 +00:00
window . FindElement ( ' _table_ ' ) . Update ( row_colors = ( ( 8 , ' white ' , ' red ' ) , ( 9 , ' black ' ) ) )
2019-07-20 02:22:49 +00:00
# sg.Popup(event, values)
2018-10-29 00:01:03 +00:00
# print(event, values)
2019-03-19 15:53:03 +00:00
window . Close ( )
2018-09-24 22:01:00 +00:00
sys . exit ( 69 )