Пример #1
0
class Gui(object):
    """ Gui for stopwatch """
    def __init__(self, stopwatch):
        """ init stopwatch gui
            stopwatch:Stopwatch -> """
        self.win = Window()  # make window an attribute
        self.stopw = stopwatch  # make stopwatch an attribute
        self.makeGui()  # create gui

    def makeGui(self):
        """ create the Gui """
        self.win.setTitle('Stopwatch v1.0')
        self.win.addStyle(
            'r.TLabel',
            foreground='red',  # create the styles
            font=('Helvetica', '30'))
        self.win.addStyle('g.TLabel',
                          foreground='green',
                          font=('Helvetica', '30'))
        self.win.addLabel('elapsed', 'Elapsed Time', style='r.TLabel')
        buttons = [('Start', self.startstop), ('Reset', self.reset),
                   ('Exit', self.win.cancel)]  # label and assign buttons
        self.win.addButton('buttons', cmd=buttons)  # create buttons
        self.win.plot('elapsed', row=0)
        self.win.plot('buttons', row=1, pady=10)
        self.update()  # update display

    def startstop(self):
        """ start or stop the stopwatch """
        if self.stopw.running:
            self.stopw.stop()
            self.win.changeWidget('buttons', 0, text='Start')  # relabel button
            self.win.changeWidget('elapsed', style='r.TLabel')  # color display
            self.win.changeState('buttons', 1, ['!disabled'])  # enable Reset
        else:
            self.stopw.start()
            self.win.changeWidget('buttons', 0, text='Stop')  # relabel button
            self.win.changeWidget('elapsed', style='g.TLabel')  # color display
            self.win.changeState('buttons', 1, ['disabled'])  # disable Reset

    def reset(self):
        """ reset stopwatch """
        self.stopw.reset()  # reset it

    def update(self):
        """ update display """
        etime = self.stopw.check()  # get elapsed time
        template = '{:02}:{:02}:{:02}.{:02}'  # 2 digits leading zero
        stime = template.format(*etime)  # format as hh:mm:ss.cc
        self.win.set('elapsed', stime)  # update display
        self.win.master.after(10, self.update)  # call again after .01 sec
Пример #2
0
class Gui(object):
    """ the GUI for the script """
    def __init__(self, mapper):
        """ create the interface """
        self.mapper = mapper
        self.dialog = Window()
        self.dialog.setTitle('Mapper 1.0')
        # notebook
        tabs = ['Routine', 'Accumulate']
        pages = self.dialog.addNotebook('notebook', tabs)
        # routine page
        self.routine = pages[0]
        today = datetime.date.today()
        self.dt = today.strftime('%d,%m,%Y,%B').split(',')
        self.routine.addEntry('title', 'Map Title', width=60)
        self.routine.set(
            'title',
            '24 Hour Precipitation Ending 7 AM {0[3]} {0[0]}, {0[2]}'.format(
                self.dt))
        self.routine.plot('title', row=0)
        self.routine.addEntry('outfile', 'Output Filename', width=40)
        self.routine.set('outfile',
                         'pcpn{0[1]}{0[0]}{0[2]}.png'.format(self.dt))
        self.routine.plot('outfile', row=1)
        jobs = ['Make KMLs', 'Make Maps']
        self.routine.addCheck('jobs', 'Jobs', jobs)
        self.routine.set('jobs', jobs[:2])
        self.routine.plot('jobs', row=2)
        # accum pcpn page
        self.accum = pages[1]
        parms = [[2, 1, 12], [2, 1, 31], [4, 2000, 2100]]
        self.accum.addSpin('endDate',
                           parms,
                           '/',
                           'Ending Date',
                           command=self.updateAccum)
        self.accum.set('endDate', [today.month, today.day, today.year])
        self.accum.plot('endDate', row=0)
        self.accum.addSpin('daysBack', [[2, 1, 45]],
                           '',
                           'Days back',
                           command=self.updateAccum)
        self.accum.set('daysBack', [2])
        self.accum.plot('daysBack', row=1)
        self.accum.addEntry('title', 'Title', width=60)
        self.accum.plot('title', row=2)
        self.accum.addEntry('outfile', 'Output Filename', width=40)
        self.accum.plot('outfile', row=3)
        self.updateAccum()
        # dialog
        self.dialog.addText('messages', 70, 15, 'Messages')
        self.dialog.plot('messages', row=1)
        self.dialog.addButton('commands', space=20)
        self.dialog.changeWidget('commands', 0, command=self.go)
        self.dialog.changeWidget('commands', 1, text='Exit')
        self.dialog.plot('commands', row=2)
        self.dialog.plot('notebook', row=0)
        self.dialog.set('notebook', 0)

    def updateAccum(self):
        """ update widgets on accum page """
        end = self.accum.get('endDate')
        endDate = datetime.date(end[2], end[0], end[1])
        endDateFmt = endDate.strftime('%d,%m,%Y,%B').split(',')
        daysBack = self.accum.get('daysBack')[0]
        self.accum.set(
            'title',
            '{0} Day Precipitation Total Ending {1[3]} {1[0]}, {1[2]}'.format(
                int(daysBack), endDateFmt))
        begDate = endDate - datetime.timedelta(
            int(self.accum.get('daysBack')[0]) - 1)
        begDateFmt = begDate.strftime('%d,%m').split(',')
        self.accum.set(
            'outfile', 'accum{0[1]}{0[0]}-{1[1]}{1[0]}{1[2]}.png'.format(
                begDateFmt, endDateFmt))

    def go(self):
        """ get current selected page and make map """
        run = self.dialog.get('notebook')  # get selected tab
        mapper = self.mapper(self)  # create a Mapper instance using the Gui
        # instance which is self
        try:
            if run == 0:
                mapper.runRoutine()
            elif run == 1:
                mapper.runAccum()
        except:
            self.dialog.set('messages', self.dialog.catchExcept())
Пример #3
0
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)