Progress Bar

From IronPython Cookbook

(Difference between revisions)
 
(5 intermediate revisions not shown)
Line 1: Line 1:
 +
A simple example of using the Windows Forms [http://msdn2.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx Progress Bar].
-
[[Image:Progress_Bar_Example.png|A 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.
 +
 
 +
 
 +
[[Image:Progress bar.jpg|A Windows Forms Progress Bar]]
<pre>
<pre>
import clr
import clr
-
 
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Windows.Forms')
-
from System.Windows.Forms import Form, ProgressBar
+
from System.Windows.Forms import Application, Form, ProgressBar
 +
from System.Threading import ThreadStart, Thread
from IronPython.Runtime.Calls import CallTarget0
from IronPython.Runtime.Calls import CallTarget0
-
from System.Threading import ThreadStart, Thread
 
class ProgressBarDialog(Form):
class ProgressBarDialog(Form):
Line 30: Line 33:
     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'
Line 46: Line 45:
 +
Application.EnableVisualStyles()
f = ProgressBarDialog()
f = ProgressBarDialog()
f.ShowDialog()
f.ShowDialog()

Current revision as of 19:37, 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 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