class Gui(object): """ A simple gui class """ def __init__(self): """ create the GUI """ categories = ['Trees', 'Birds', 'Flowers'] self.gui = Window() self.gui.setTitle('Dynamic Widget Demo') self.gui.addRadio('category', 'Item Types', categories) self.gui.addCombo('items', 'Items', None, postcommand=self.update) self.gui.addButton('command') self.gui.set('category', 'Trees') self.gui.set('items', '...') self.gui.plot('category', row=0) self.gui.plot('items', row=1, pady=20) self.gui.plot('command', row=2) def update(self): # callback function """ set the combobox values by what is in the radio button box """ lookup = { 'Trees': ['Oak', 'Maple', 'Beech'], 'Birds': ['Cardinal', 'Robin', 'Sparrow'], 'Flowers': ['Rose', 'Petunia', 'Daylily'] } select = self.gui.get('category') self.gui.set('items', lookup[select], allValues=True)
class Gui(object): """ The Tornado Path Plotting GUI """ def __init__(self): """ create the GUI """ counties = ['Clark','Crawford','Dubois','Floyd','Harrison','Jefferson', 'Orange','Perry','Scott','Washigton'] damage = ['EF0','EF1','EF2','EF3','EF4','EF5'] dateParms = [[2,1,12],[2,1,12],[4,1900,2100]] initDate = [1,1,1980] cols = [['Date', 100],['County', 100],['Damage', 100]] self.gui = Window() self.gui.setTitle('Tornado Path Generator') self.gui.addSpin('tdate', dateParms, '/', 'Date of Tornado') self.gui.set('tdate', initDate) self.gui.addCombo('county', 'Affected County', counties) self.gui.addRadio('level', 'Maximum EF Damage', damage) self.gui.addCollector('paths', 10, cols, ['tdate','county','level'], 'Included Tornadoes') self.gui.addButton('command') self.gui.plot('tdate', row=0, pady=5) self.gui.plot('county', row=1, pady=5) self.gui.plot('level', row=2, pady=5) self.gui.plot('paths', row=3, pady=5) self.gui.plot('command', row=4, pady=10)
def update(gui): # callback function """ set the alist attribute by what is in the radio button box """ lookup = { 'Trees': ['Oak', 'Maple', 'Beech'], 'Birds': ['Cardinal', 'Robin', 'Sparrow'], 'Flowers': ['Rose', 'Petunia', 'Daylily'] } select = gui.get('category') gui.set('items', lookup[select], allValues=True) categories = ['Trees', 'Birds', 'Flowers'] gui = Window() gui.setTitle('Dynamic Widget Demo') gui.addRadio('category', 'Item Types', categories) gui.addCombo('items', 'Items', None, postcommand=(lambda: update(gui))) gui.addButton('command') gui.set('category', 'Trees') gui.set('items', '...') gui.plot('category', row=0) gui.plot('items', row=1, pady=20) gui.plot('command', row=2) gui.waitforUser() if gui.content: selected_cat = gui.get('category') item = gui.get('items') # more code would go here... gui.cancel()
class Gui(object): def __init__(self): # read configuration file self.config = configparser.ConfigParser() self.config.read('config_parameters.ini') # write gui self.gui = Window() self.gui.setTitle('Export ScanInfo to Gsheet') self.gui.addChooseDir('dir_date', 'Scan Date Folder', width=40, initialdir='') self.gui.addEntry('para_list', 'Parameters to list', width=50) self.gui.addCombo('gdir', 'Save location in google drive', None, postcommand=self.gdir_update, width=47) self.gui.set('para_list', self.config['DEFAULT']['parameters']) self.gui.addText('status', width=40, height=5, prompt='Status:') self.gui.addCheck('auto_update', '', ['Turn On Auto Update']) self.gui.addButton('commands') self.gui.changeWidget('commands', 0, text='Export', command=self.export) self.gui.plot('dir_date', row=0) self.gui.plot('para_list', row=1, pady=5) self.gui.plot('gdir', row=2, pady=5) self.gui.plot('status', row=3, pady=10) self.gui.plot('auto_update', row=4) self.gui.plot('commands', row=5, pady=10) self.exported = False self.proj = None # bella project name self.g_name = None # google drive folder name self.g_ID = None # google drive folder id self.autoupdateOn = False self.update() def get_dirname_id(proj): '''Get the name and ID of google drive save locaion from google_dir.ini proj:''' config_g = configparser.ConfigParser() config_g.read('google_dir') name = [ config_g[i]['name'] for i in config_g.sections() if config_g[i]['proj'] == proj ] ID = [ config_g[i]['id'] for i in config_g.sections() if config_g[i]['proj'] == proj ] return name, ID def gdir_update(self): '''Update a saving location option on GUI, depending on your project''' # get BELLA project name dir_date = self.gui.get('dir_date', allValues=True) self.proj = get_proj_name(dir_date) # get google drive folder name and ID from .ini file cfg = configparser.ConfigParser() cfg.read('config_gdrive.ini') self.g_name = [ cfg[i]['name'] for i in cfg.sections() if cfg[i]['proj'] == self.proj ] self.g_ID = [ cfg[i]['id'] for i in cfg.sections() if cfg[i]['proj'] == self.proj ] # update save folder options in GUI self.gui.set('gdir', self.g_name, allValues=True) def export(self): '''When Export button is pressed, scan info is written into a google sheet''' # Get settings from GUI dir_date = self.gui.get('dir_date', allValues=True) # directory of scandata para_list = self.gui.get('para_list', allValues=True) # parameters to save gdrive_name = self.gui.get('gdir') # name of the google drive if not dir_date or not gdrive_name: self.gui.set('status', 'Please fill in all sections\n') else: gdrive_id = self.g_ID[self.g_name.index( gdrive_name)] # google drive ID sheet_title = self.proj + ' ' + os.path.basename( dir_date) + ' ScanSummary' # write self.scaninfo = scaninfo2gsheet(dir_date, para_list) sheet = self.scaninfo.write(gdrive_id, sheet_title) self.exported = True # add text to status window message = 'Gsheet \'' + sheet.title + '\' saved\nURL: ' + sheet.url self.gui.set('status', message) # update the config file self.write_config(para_list, dir_date) def update(self): '''Update google sheet every minute.''' if bool(self.gui.get('auto_update')) and self.exported: # if autoupdate is turned on, message appears on status window if not self.autoupdateOn: self.gui.set('status', '\nAuto update On...') self.autoupdateOn = True nscan_new = self.scaninfo.update() # if there is a new scan, add text to the status window if nscan_new: message = '\nScan ' + str(nscan_new) + ' updated' self.gui.set('status', message) else: # if autoupdate is turned off, message appears on status window if self.autoupdateOn: self.gui.set('status', '\nAuto update Off') self.autoupdateOn = False self.gui.master.after(30 * 1000, self.update) # wait for 30 seconds def write_config(self, para_list, dir_date): '''Save updated parameter list in config.ini file''' self.config['DEFAULT']['parameters'] = para_list with open('config_parameters.ini', 'w') as configfile: self.config.write(configfile)