sg.Col([ ss.record('Item', 'name'), ss.record('Item', 'fkMenu', sg.Combo), ss.record('Item', 'price'), ss.record('Item', 'description', sg.MLine, (30, 7)) ]) ], ss.record_navigation('Item', navigation=False, search=False)] layout += [[sg.Frame('Items', sub_layout)]] layout += [ ss.record_navigation('Restaurant', protect=True, search=True, save=True) ] # Initialize our window and database, then bind them together win = sg.Window('places to eat', layout, finalize=True) db = ss.Database('example2.db', win, sql_file='example2.sql' ) # <=== load the database and bind it to the window # Set our callbacks db.set_callback('edit_enable', en) db.set_callback('edit_disable', dis) while True: event, values = win.read() if db.process_events( event, values): # <=== let PySimpleSQL process its own events! Simple! print('PySimpleDB event handler handled the event!') elif event == sg.WIN_CLOSED or event == 'Exit': db = None # <= ensures proper closing of the sqlite database and runs a database optimization break else:
# CREATE PYSIMPLEGUI LAYOUT # ------------------------- # Define the columns for the table selector headings=['id','Date: ','Mood: ','Title: '] # The width of the headings defines column width! visible=[0,1,1,1] # Hide the id column layout=[ ss.selector('sel_journal','Journal',sg.Table,num_rows=10,headings=headings,visible_column_map=visible), ss.actions('act_journal','Journal', edit_protect=False), # These are your database controls (Previous, Next, Save, Insert, etc!) ss.record('Journal.entry_date', label='Date:'), ss.record('Journal.mood_id', sg.Combo, label='My mood:', size=(30,10), auto_size_text=False), ss.record('Journal.title', size=(71,1)), ss.record('Journal.entry', sg.MLine, size=(71,20)) ] win=sg.Window('Journal example', layout, finalize=True) db=ss.Database('journal.db', win, sql_commands=sql) #<=== ONE SIMPLE CHANGE!!! # Now we just give the new databasase a name - "journal.db" in this case. If journal.db is not present # when this code is run, then a new one is created using the commands supplied to the sql_commands keyword argument. # If journal.db does exist, then it is used and the sql_commands are not run at all. # Reverse the default sort order so new journal entries appear at the top db['Journal'].set_order_clause('ORDER BY entry_date DESC') # Set the column order for search operations. By default, only the column designated as the description column is searched db['Journal'].set_search_order(['entry_date','title','entry']) # --------------- # DATA VALIDATION # --------------- def cb_validate(): date=win['Journal.entry_date'].Get() if date[4] == '-' and date[7]=='-' and len(date)==10: # Make sure the date is 10 digits and has two dashes in the right place
ss.record('Item.description', sg.MLine, (30, 7)) ]) ], ss.actions('act_item', 'Item', edit_protect=False, navigation=False, save=False, search=False) ] layout += [[sg.Frame('Items', sub_layout)]] layout += [ss.actions('act_restaurant', 'Restaurant')] # Initialize our window and database, then bind them together win = sg.Window('places to eat', layout, finalize=True) db = ss.Database(':memory:', win, sql_script='example.sql' ) # <=== load the database and bind it to the window # NOTE: ":memory:" is a special database URL for in-memory databases # Set our callbacks # See documentation for a full list of callbacks supported db.set_callback('edit_enable', enable) db.set_callback('edit_disable', disable) while True: event, values = win.read() if db.process_events( event, values): # <=== let PySimpleSQL process its own events! Simple! logger.info('PySimpleDB event handler handled the event!') elif event == sg.WIN_CLOSED or event == 'Exit':
visible_column_map=visible), ss.actions( 'act_journal', 'Journal', edit_protect=False ), # These are your database controls (Previous, Next, Save, Insert, etc!) ss.record('Journal.entry_date', label='Date:'), ss.record('Journal.mood_id', sg.Combo, label='My mood:', size=(30, 10), auto_size_text=False), ss.record('Journal.title', size=(71, 1)), ss.record('Journal.entry', sg.MLine, size=(71, 20)) ] win = sg.Window('Journal example', layout, finalize=True) db = ss.Database(':memory:', win, sql_commands=sql) #<=== Here is the magic! # Note: ':memory:' is a special address for in-memory databases # Reverse the default sort order so new journal entries appear at the top db['Journal'].set_order_clause('ORDER BY entry_date DESC') # Set the column order for search operations. By default, only the column designated as the description column is searched db['Journal'].set_search_order(['entry_date', 'title', 'entry']) # --------- # MAIN LOOP # --------- while True: event, values = win.read() if db.process_events( event,
sg.Table, num_rows=10, headings=headings, visible_column_map=visible), ss.actions('act_journal', 'Journal'), ss.record('Journal.entry_date'), ss.record('Journal.mood_id', sg.Combo, label='My mood:', size=(30, 10), auto_size_text=False), ss.record('Journal.title'), ss.record('Journal.entry', sg.MLine, size=(71, 20)) ] win = sg.Window('Journal example', layout, finalize=True) db = ss.Database('journal.db', win, sql_script='journal.sql') #<=== Here is the magic! # Note: sql_script is only run if journal.db does not exist! This has the effect of creating a new blank # database as defined by the sql_script file if the database does not yet exist, otherwise it will use the database! # Reverse the default sort order so new journal entries appear at the top db['Journal'].set_order_clause('ORDER BY entry_date DESC') # Set the column order for search operations. By default, only the column designated as the description column is searched db['Journal'].set_search_order(['entry_date', 'title', 'entry']) # --------- # MAIN LOOP # --------- while True: event, values = win.read() if db.process_events(
# ------------------------- # CREATE PYSIMPLEGUI LAYOUT # ------------------------- # Define the columns for the table selector headings=['id','Date: ','Mood: ','Title: '] visible=[0,1,1,1] # Hide the id column layout=[ ss.selector('sel_journal','Journal',sg.Table,num_rows=10,headings=headings,visible_column_map=visible), ss.actions('act_journal','Journal'), ss.record('Journal.entry_date'), ss.record('Journal.mood_id', sg.Combo, label='My mood:', size=(30,10), auto_size_text=False), ss.record('Journal.title'), ss.record('Journal.entry', sg.MLine, size=(71,20)) ] win=sg.Window('Journal example', layout, finalize=True) db=ss.Database('journal.db', win, sql_commands=sql) #<=== Here is the magic! # Note: sql_commands in only run if journal.db does not exist! This has the effect of creating a new blank # database as defined by the sql_commands if the database does not yet exist, otherwise it will use the database! # Reverse the default sort order so new journal entries appear at the top db['Journal'].set_order_clause('ORDER BY entry_date DESC') # Set the column order for search operations. By default, only the column designated as the description column is searched db['Journal'].set_search_order(['entry_date','title','entry']) # --------- # MAIN LOOP # --------- while True: event, values = win.read() if db.process_events(event, values): # <=== let PySimpleSQL process its own events! Simple!
label='Color:', element=sg.Combo, size=(30, 10), auto_size_text=False) ] layout = [[sg.Frame('Person Editor', layout=person_layout)], [sg.Frame('Color Editor', layout=color_layout)], [ sg.Frame('Everyone can have multiple favorite colors!', layout=favorites_layout) ]] # Initialize our window and database, then bind them together win = sg.Window('Many-to-many table test', layout, finalize=True) db = ss.Database( ':memory:', win, sql_commands=sql) # <=== load the database and bind it to the window # NOTE: ":memory:" is a special database URL for in-memory databases while True: event, values = win.read() if db.process_events( event, values): # <=== let PySimpleSQL process its own events! Simple! logger.info('PySimpleDB event handler handled the event!') elif event == sg.WIN_CLOSED or event == 'Exit': db = None # <= ensures proper closing of the sqlite database and runs a database optimization at close break else: logger.info(f'This event ({event}) is not yet handled.')
[sg.Text('')], ss.record('Settings.value?key=debug_mode', sg.CBox), [sg.Text('')], ss.record('Settings.value?key=antialiasing', sg.CBox), [sg.Text('')], ss.record('Settings.value?key=query_retries'), # For the actions, we don't want to offer users to insert or delete records from the settings table, # and there is no use for navigation buttons due to the key,value nature of the data. Therefore, we will # disable all actions (default=False) except for the Save action (save=True) ss.actions('nav', 'Settings', default=False, save=True) ] # Initialize our window and database, then bind them together win = sg.Window('Preferences: Application Settings', layout, finalize=True) db = ss.Database( 'Settigs.db', win, sql_commands=sql) # <=== load the database and bind it to the window # Now that the database is loaded, lets set our tool tips using the description column. # The Table.get_keyed_value can return the value column where the key column equals a specific value as well. win['Settings.value?key=company_name'].set_tooltip( db['Settings'].get_keyed_value('description', 'key', 'company_name')) win['Settings.value?key=debug_mode'].set_tooltip( db['Settings'].get_keyed_value('description', 'key', 'debug_mode')) win['Settings.value?key=antialiasing'].set_tooltip( db['Settings'].get_keyed_value('description', 'key', 'antialiasing')) win['Settings.value?key=query_retries'].set_tooltip( db['Settings'].get_keyed_value('description', 'key', 'query_retries')) while True: event, values = win.read()