Progress Bar

From IronPython Cookbook

(Difference between revisions)
Line 34: Line 34:
     def startProgress(self, s, e):
     def startProgress(self, s, e):
         def update():
         def update():
-
             x = 1
+
             for i in range(100)
-
            while True:
+
                 print i
-
                 print x
+
                 def step():
                 def step():
-
                     self.prog.Value = x
+
                     self.prog.Value = i + 1
                 self.Invoke(CallTarget0(step))
                 self.Invoke(CallTarget0(step))
                 Thread.Sleep(30)
                 Thread.Sleep(30)
-
                x += 1
 
-
                if x > 100:
 
-
                    break
 
                      
                      
             print 'Done'
             print 'Done'

Revision as of 17:02, 23 August 2007

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 Form, ProgressBar
from IronPython.Runtime.Calls import CallTarget0

from System.Threading import ThreadStart, Thread

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()


f = ProgressBarDialog()
f.ShowDialog()


Back to Contents.

TOOLBOX
LANGUAGES