473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sample code available for progress bar

440 256MB
Hi,

Can anybody help me in creating a progress bar in the UI using Python -TKinter.


Thanks
PSB
Mar 10 '07 #1
4 3055
bartonc
6,596 Expert 4TB
Here's one that I wrote about a million years ago. Good for example and it works, but I can no longer vouch for the style:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from BCDbaseDialog import baseDialog
  3.  
  4. class progressDialog(baseDialog):
  5.  
  6.     def __init__(self, root, label=""):
  7.         baseDialog.__init__(self, root, title='Progress', label=label)
  8.         self.canceled = False
  9.  
  10.     def CreateWidgets(self, top, label):
  11.         self.frameMain = frameMain = Frame(top, bg=self.bg, borderwidth=2, relief=SUNKEN)
  12.         Label(frameMain, bg=self.bg, text=label, width=24
  13.               ).grid(row=0, column=0, sticky=EW, columnspan=6, padx=5, pady=10)
  14.  
  15.         self.meter_x = meter_x = 2
  16.         self.meter_len = meter_len = 244
  17.         self.meter = meter = Canvas(frameMain, bg=self.bg, width=meter_len, height=18)
  18.         meter.grid(row=1, column=0, sticky=EW, columnspan=6, padx=20, pady=5)
  19.         self.rect = meter.create_rectangle(2, 2, meter_x, 20, fill="black")
  20.  
  21.         button = Button(frameMain, text='Cancel', command=self.Cancel, takefocus=0)
  22.         button.grid(row=2, column=5, sticky=EW, columnspan=1, padx=15, pady=10)
  23.         frameMain.grid(row=0, column=0) # I tried to get rid of flashing here
  24.  
  25.     def Cancel(self, event=None):
  26.         self.close()
  27.  
  28.     def Run(self, progress):
  29.         delta_p = progress - self.oldprog
  30.         if delta_p >= self.delta_p:
  31.             self.oldprog = progress
  32.             self.meter_x += self.delta_x
  33.             self.meter.coords(self.rect, 2, 2, self.meter_x, 20)
  34.             self.top.update()
  35.         return self.canceled
  36.  
  37.     def SetMax(self, count):
  38.         self.count = count
  39.         if count > self.meter_len:
  40.             self.delta_x = 1
  41.             self.delta_p = count//self.meter_len + 1
  42.         else:
  43.             self.delta_p = 1
  44.             self.delta_x = self.meter_len//count
  45.         self.oldprog = 0
  46.  
  47.  
  48.     def close(self, event=None):
  49.         self.canceled = True
  50.         self.top.destroy()
  51.  
  52. ##################################
  53.  
  54.     def AddTestButton(self):
  55.         button = Button(self.frameMain, text='test', command=self.test, takefocus=0)
  56.         button.grid(row=2, column=0, sticky=EW, columnspan=1, padx=15, pady=10)
  57.  
  58.     def test(self):
  59.         from time import sleep
  60.         import tkSimpleDialog
  61.         count = tkSimpleDialog.askinteger('Test', 'How many times?', parent=self.top)
  62.         self.SetMax(count)
  63.         st = max(1.0/count * 2, .001)
  64.         for loop in range(count):
  65.             canceled = self.Run(loop)
  66.             if canceled:
  67.                 print 'canceled'
  68.                 break
  69.             sleep(st)
  70.         if not canceled:
  71.             self.close()
  72.         root.destroy()
  73.  
  74.  
  75. if __name__ == '__main__':
  76.     # test the dialog
  77.     root = Tk()
  78.     testDialog = progressDialog(root, 'Cross Referencing')
  79.     testDialog.AddTestButton()
  80.     root.mainloop()
  81. #    root.destroy()
  82.  
Mar 10 '07 #2
psbasha
440 256MB
I am getting the following error.I need the module BCDbaseDialog to execute the Progress bar.

Thanks & Regards
PSB

>>>

Traceback (most recent call last):
File "C:\Shakil\SAmpleCases\SampleProgress.py", line 2, in -toplevel-
from BCDbaseDialog import baseDialog
ImportError: No module named BCDbaseDialog
Mar 12 '07 #3
bartonc
6,596 Expert 4TB
I am getting the following error.I need the module BCDbaseDialog to execute the Progress bar.

Thanks & Regards
PSB

>>>

Traceback (most recent call last):
File "C:\Shakil\SAmpleCases\SampleProgress.py", line 2, in -toplevel-
from BCDbaseDialog import baseDialog
ImportError: No module named BCDbaseDialog
Sorry about that. Here's that one:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from tkSimpleDialog import _QueryDialog as QueryDialog
  3. from tkSimpleDialog import Dialog
  4.  
  5.  
  6. class baseDialog:
  7.     """Basic dialog window: baseDialog.frameMain is where to put widgets.
  8.     Pass in option title and label; label will span three columns in the
  9.     top row. Over-ride CreateWidgets(self, top, label) to add widgets.
  10.     """
  11.     def __init__(self, root, title="", label=""):
  12.         self.root = root
  13.         self.top = top = Toplevel(root)
  14.         top.configure(borderwidth=2)
  15.         top.geometry("+%d+%d" % (root.winfo_screenwidth()/2,
  16.                                   root.winfo_screenheight()/3))
  17.         self.bg = "white"
  18.         self.fg = "black"
  19.         self.CreateWidgets(top, label)              # Fit widgets into self
  20.         top.resizable(height=FALSE, width=FALSE)    # then lock the size
  21.         top.title(title)
  22.         top.protocol("WM_DELETE_WINDOW", self.close)
  23.         top.update()
  24. #        top.tkraise()
  25.         top.wm_deiconify()
  26.  
  27.     def CreateWidgets(self, top, label):
  28.         self.frameMain = frameMain = Frame(top, bg=self.bg, borderwidth=2, relief=SUNKEN)
  29.         frameMain.grid(row=0, column=0)
  30.         if label:
  31.             Label(frameMain, text=label, bg=self.bg, width=24
  32.                   ).grid(row=0, column=0, sticky=EW, columnspan=3, padx=5, pady=5)
  33.  
  34.  
  35.     def close(self, event=None):
  36.         self.top.destroy()
  37.  
  38.  
  39. class QueryHex(QueryDialog):
  40.  
  41.     errormessage = "Not a hex value"
  42.  
  43.     def getresult(self):
  44.         return int(self.entry.get(), 16)
  45.  
  46.     def validate(self):
  47.         if QueryDialog.validate(self):
  48.             self.result = hex(self.result)[2:].zfill(4)
  49.             return 1
  50.         return 0
  51.  
  52. def askhex(title, prompt, **kw):
  53.     '''get an integer from the user
  54.  
  55.     Arguments:
  56.  
  57.         title -- the dialog title
  58.         prompt -- the label text
  59.         **kw -- see SimpleDialog class
  60.  
  61.     Return value is an integer
  62.     '''
  63.     d = QueryHex(title, prompt, **kw)
  64.     return d.result
  65.  
  66.  
  67. class QueryStartEndValue(QueryDialog):
  68. ##    def __init__(self, title, prompt,
  69. ##                 initialvalue=None,
  70. ##                 minvalue = None, maxvalue = None,
  71. ##                 parent = None):
  72.  
  73.     def body(self, mainFrame):
  74.         w = Label(mainFrame, text=self.prompt, justify=LEFT)
  75.         w.grid(row=0, padx=0, sticky=W, columnspan=4)
  76.  
  77.         Label(mainFrame, text="Start").grid(row=1, column=0, sticky=W, padx=5)
  78.         Label(mainFrame, text="End").grid(row=1, column=1, sticky=W, padx=5)
  79.         Label(mainFrame, text="Value").grid(row=1, column=2, sticky=W, padx=5)
  80.  
  81.         self.start = pyvar = StringVar(self)
  82.         self.startEntry = entry = Entry(mainFrame, textvariable=pyvar, width=5)
  83.         entry.grid(row=2, column=0, sticky=EW, padx=5)
  84.         self.end = pyvar = StringVar(self)
  85.         self.endEntry = entry = Entry(mainFrame, textvariable=pyvar, width=5)
  86.         entry.grid(row=2, column=1, sticky=EW, padx=5)
  87.         self.value = pyvar = StringVar(self)
  88.         self.valueEntry = entry = Entry(mainFrame, textvariable=pyvar, width=3)
  89.         entry.grid(row=2, column=2, sticky=W, padx=5)
  90.         if self.initialvalue:
  91.             pyvar.set(self.initialvalue)
  92.         else:
  93.             pyvar.set('FF')
  94.         return self.startEntry
  95.  
  96.     def validate(self):
  97.         maxvalue = self.maxvalue or 0xffff
  98.         minvalue = self.minvalue or 0
  99.         a = self.start.get()
  100.         b = self.end.get()
  101.         c = self.value.get()
  102.         if a or b:
  103.             try:
  104.                 self.startEntry.select_range(0, END)
  105.                 self.initial_focus = self.startEntry
  106.                 i = int(a, 16)
  107.                 if i < minvalue or i > maxvalue:
  108.                     raise ValueError
  109.                 self.endEntry.select_range(0, END)
  110.                 self.initial_focus = self.endEntry
  111.                 a = hex(i)[2:].zfill(4)
  112.                 if b:
  113.                     j = int(b, 16)
  114.                     if j < minvalue or j < i or j > maxvalue:
  115.                         raise ValueError
  116.                 else:
  117.                     raise ValueError
  118.                 self.valueEntry.select_range(0, END)
  119.                 self.initial_focus = self.valueEntry
  120.                 b = hex(j)[2:].zfill(4)
  121.                 if c:
  122.                     i = int(c, 16)
  123.                     if i > 0xff:
  124.                         raise ValueError
  125.                     c = hex(i)[2:].zfill(2)
  126.                 else:
  127.                     self.value.set('FF')
  128.                     self.valueEntry.select_range(0, END)
  129.                     raise ValueError
  130.             except ValueError:
  131.                 self.bell()
  132.                 return False
  133.         self.result = (a, b, c)
  134.         return True
  135.  
  136.  
  137. def askblockvalue(title, prompt='', initialvalue=None,
  138.                   minvalue=0, maxvalue=0x7fff, parent=None):
  139.     d = QueryStartEndValue(title, prompt, initialvalue, minvalue, maxvalue, parent)
  140.     return d.result
  141.  
  142. if __name__ == '__main__':
  143.     # test the dialog
  144.     root = Tk()
  145. ##    def dialog():
  146. ##        baseDialog(root, 'test', 'test')
  147. ##    def dialog():
  148. ##        print askhex("Dump Address", "Enter starting address", minvalue=0, maxvalue=0xffff)
  149.     def dialog():
  150.         print repr(askblockvalue('Block Fill', 'Use hex: Start < End < $8000', parent=root))
  151.     Button(root, text='Dialog', command=dialog).pack()
  152.     root.mainloop()
  153. ##    if root:
  154. ##        root.destroy()
  155.  
Mar 12 '07 #4
psbasha
440 256MB
Thanks for the sample code.

How can I identify the % of completion of the task thru my progress bar?.Since I will be doing different tasks by calling methods and I have to push my progress status by so much %.


Thanks
PSB
Mar 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian | last post by:
I have try using ant-contrib-03.jar sample from Source Forge, I was not able to use it. It complains about the following: foreach, it complains that the trim is not available. propertycopy, it...
1
by: angelina | last post by:
Hi, Can anyone help me write some code that will run a progress bar before a new form is loaded? The progressbar does not have to represent the actual progress of the form load but simply...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
1
by: Andrew | last post by:
Hi Group! I'm looking for a tutorial in using SQL-DMO, or some sample code that will allow me to let my users switch the SQL Server database that the front end app is linking to as a back end. ...
6
by: Daniel Rimmelzwaan | last post by:
I want to send a biztalk document to an aspx page, and I need to see some sample code, because I just can't make it work. I have a port with transport type HTTP, pointing to my aspx page, something...
2
by: bob | last post by:
Hello, I want to show progress to the user while some method is running so I thought I'd use a progress bar. But I don't see how I can do this without writing GUI code in the model? In...
30
by: Chad Z. Hower aka Kudzu | last post by:
I need to provide a series of demos for an assembly. There are potentialy several dozen of them. None of them are very complex, however maintaining two versions of them will be very maintainance...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.