Progress Bar

From IronPython Cookbook

A simple example of using the Windows Forms Progress Bar.

The progress bar is in a form shown modally (shown with ShowDialog). Another thread is launched, which periodically updates the progress bar - which it has to do by invoking back onto the control thread.


A Windows Forms Progress Bar

import clr
clr.AddReference('System.Windows.Forms')

from System.Windows.Forms import Application, Form, ProgressBar
from System.Threading import ThreadStart, Thread
from IronPython.Runtime.Calls import CallTarget0


class ProgressBarDialog(Form):
    def __init__(self):
        self.Text = 'Progress Bar Example'
        pb = ProgressBar()
        pb.Minimum = 1
        pb.Maximum = 100
        pb.Step = 1
        pb.Value = 1
        
        pb.Width = 400
        self.Controls.Add(pb)
        self.prog = pb
        
        self.Shown += self.startProgress
        
        
    def startProgress(self, s, e):
        def update():
            for i in range(100):
                print i
                def step():
                    self.prog.Value = i + 1
                self.Invoke(CallTarget0(step))
                Thread.Sleep(30)
                    
            print 'Done'
        t = Thread(ThreadStart(update))
        t.Start()


Application.EnableVisualStyles()
f = ProgressBarDialog()
f.ShowDialog()


Back to Contents.

TOOLBOX
LANGUAGES