Home > wxpython > wx.ProgressDialog: progressBar riutilizzabile

wx.ProgressDialog: progressBar riutilizzabile

11 Marzo 2011

Questo codice mi è comodo quando voglio visualizzare ua progress
bar, nel momento in cui sto inserendo dati ad esempio, in un
database.
Di seguito il codice:

'''Module containing a Progress Bar class'''

import wx

class Data(object):
    '''Data class for M-V-C pattern'''
    def __init__(self):
        self.count = 0
        self.max_v = 0
    def add_count (self):
        '''increase the progress bar count for showing its progress'''
        self.count += 1
    def get_max(self):
        '''Get the max value of the progress bar'''
        return self.max_v
    def get_count(self):
        '''Get the current count value of the progressbar in the loop'''
        return self.count
    def set_max(self, value):
        '''set the max value of the progress bar'''
        self.max_v = int(value)

class ProgressBar(wx.ProgressDialog):
    '''Progress Dialog constructor'''
    def __init__(self, max_v):
        wx.ProgressDialog.__init__(self, "", "Update...", max_v,
                                   style = wx.PD_AUTO_HIDE |
                                   wx.PD_ELAPSED_TIME |
                                   wx.PD_REMAINING_TIME)
    def progress(self, progress):
        '''update the progress bar during the loops'''
        wx.MicroSleep(1) #for short processes use wx.MilliSleep(1)
        self.Update(progress)
    def destroy(self):
        '''Distruttore della progress bar'''
        self.Destroy()

class BarController(object):
    '''Progress Bar Controller Constructor'''
    def __init__(self, value = 3):
        self.data = Data()
        self.data.set_max(value)
        self.max = self.data.get_max()
        self.count = self.data.get_count()
        self.progress_bar = ProgressBar(self.max)
    def update_progress_bar(self):
        '''Aggiorna la progressBar'''
        self.data.add_count()
        self.count = self.data.get_count()
        self.progress_bar.progress(self.count) # update progressbar
    def destroy_progress_bar(self):
        '''Destroy the wx.ProgressDialog instance'''
        self.progress_bar.destroy()

def main():
    '''app starter'''
    app = wx.PySimpleApp()
    bar_c = BarController(600)
    while bar_c.count < bar_c.max:
        bar_c.update_progress_bar()
    bar_c.destroy_progress_bar()
    app.MainLoop()
    
if __name__ == "__main__":
    main()

in pratica, utilizzo un controller e gli passo come
argomento un numero, che possono essere le righe di un
file che sto parsando, la lunghezza di una lista ecc.
Questo numero, viene settato nella class Data, sulla
variabile ‘max_v’ ed utilizzato come limite per il finecorsa della
progress bar. Ad ogni ciclo (while nell’esempio), la variabile
count aumenta di uno, fino al raggiungimento del valore massimo
(fine corsa della bar).

Messo il tutto in un modulo, è comodo poterlo richiamare, ogni
volta si decida di visualizzare una progress bar.

NOTA:

se l’attesa in secondi tra un ciclo e l’altro (wx.Sleep(int))
snerva troppo, utilizzare il metodo wx.MilliSleep(int)
ed accellerare il tutto.

EDIT:
ho rivisto il codice dopo le “bacchettate” di PyLint

Categorie:wxpython Tag:
I commenti sono chiusi.