51168

Using the same progressbar in tkinter for several computation

Question:

I wish to know how is the best method to use the same progressbar (determinate and indeterminate). In this example the same progressbar is used for a indeterminate, for a determinate, and function-indeterminate computation. When i run the code only the last progressbar is showed.

from Tkinter import * import ttk import tkFileDialog import time def foo(m, n, self_from_class): for i in xrange(m): i * n self_from_class.pbar_f.step(1) self_from_class.update() time.sleep(0.1) return i class MainWindow(Frame): def __init__(self): Frame.__init__(self) self.master.title("ProgressBar example") self.master.minsize(200, 100) self.grid(sticky=E+W+N+S) top = self.winfo_toplevel() top.rowconfigure(0, weight=1) top.columnconfigure(0, weight=1) self.start = Button(self, text='Start', command=self.start, activeforeground="red") self.start.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S) self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate") self.pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate") self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate") self.pbar_f.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) def start(self): for i in xrange(10): self.pbar_ind.step(1) self.update() # Busy-wait time.sleep(0.1) for i in xrange(10): self.pbar_ind.step(1) self.update() # Busy-wait time.sleep(0.1) res = foo(10, 2, self) if __name__=="__main__": d = MainWindow() d.mainloop()

UPDATE - my not elegant solution is:

class MainWindow(Frame): def __init__(self): Frame.__init__(self) self.master.title("ProgressBar example") self.master.minsize(200, 100) self.grid(sticky=E+W+N+S) top = self.winfo_toplevel() top.rowconfigure(0, weight=1) top.columnconfigure(0, weight=1) self.start = Button(self, text='Start', command=self.start, activeforeground="red") self.start.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S) self.pbar_f = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate") self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=300, mode="determinate") self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=300, mode="indeterminate") self.pbar_ind.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) def start(self): for i in xrange(10): self.pbar_ind.step(1) self.update() # Busy-wait time.sleep(0.1) self.pbar_ind.grid_forget() self.pbar_det.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) for i in xrange(10): self.pbar_det.step(1) self.update() # Busy-wait time.sleep(0.1) self.pbar_det.grid_forget() self.pbar_f.grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) res = foo(10, 2, self)

Answer1:

If you put all widgets in one cell - they have the same column and row - then you put them one over another. Last one is on the top and you see it.

You can use only one Progressbar widget - for all computation.

You can have few Progressbars and use grid/grid_forget to show one and hide anothers.

You can put Progressbars in different cells and have all on the screen.

<hr />

<strong>EDIT:</strong>

example with grid_forget

from Tkinter import * #----------------------------------- def on_press(): global visible if visible == 1: l1.grid_forget() l3.grid(row=0,column=3) visible = 3 else: l1.grid(row=0,column=0) l3.grid_forget() visible = 1 #----------------------------------- master = Tk() l1 = Button(master, text='press there >>') l1.grid(row=0,column=0) l2 = Button(master, text='change', command=on_press) l2.grid(row=0,column=1) l3 = Button(master, text='<< press there') #l3.grid(row=0,column=3) visible = 1 master.mainloop()

Recommend

  • python tkinter Entry widget Dynamic Name
  • Cannot use the output of the inner (child ) template in the parent template in cloudformation
  • How can I indent all text in a paragraph except the first line?
  • android: Unable to pause activity : java.lang.IllegalArgumentException: Service not registered:
  • Calling super.approveSelection() within a SwingWorker
  • CSS Bundling and Internet Explorer's Limit
  • Unspecified error could not find a writer
  • Putting progress bar inside edit text which will launch after user has entered something
  • How to fetch the entry text on a different window?
  • QStandardItemModel::removeRows() does not work in my use case
  • change volume of mp3 playing via wmplib in c#
  • How to start server for Selenium grid Java Maven setup
  • Allocating a 2D contiguous array within a function
  • Python Tkinter after event OOPS implementation
  • Python function to read variable length blocks of data from file while open
  • MRO with multiple inheritance in python
  • How to move an element within a structure, possibly with zippers?
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Fully customized Python Help Usage
  • SPARQL date range
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Dynamically load css stylesheet and wait for it to load
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • Django foreign key drop down
  • How to get links to open in the native browser in iOS Meteor apps?
  • error importing numpy
  • Grails calculated field in SQL
  • Ajax Loaded meta Tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display issues when we change from one jquery mobile page to another in firefox
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • To display the title for the current loaction in map in iphone
  • Return words with double consecutive letters
  • How do I rollback to a specific git commit
  • trying to dynamically update Highchart column chart but series undefined
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize