Question

I am using itertools.combinations module to find a large number of combinations. While my program finds all its combinations (a lot of them) it checks for sum of every combination to be some number and then program store that combination in list.

from itertools import *
from math import *
import Tkinter as tk
import ttk


x = int(raw_input('Enter number of combinations: '))
z = int(raw_input('Enter sum number: '))


def combinator():
    comb = combinations(range(100), x)
    for i in comb:
        yield i

my_combinations = []
combination_s = combinator()
for i in combination_s:
    print i
    c = list(i)
    if fsum(c)==z:
        my_combinations.append(c)

print my_combinations

root = tk.Tk()
root.title('ttk.Progressbar')
pbar = ttk.Progressbar(root, length=300, mode='determinate', maximum = 100)
pbar.pack(padx=5, pady=5)
root.mainloop()

I want to have ttk.progressbar that shows progress every time program evaluates sum of combinations. How can I do that?

Was it helpful?

Solution

Here is an example that increases the progress bar for every combination. It just waits for a short time, but you can easily change it to do some calculations in the for loop in ProgBarApp.start instead of time.sleep

from Tkinter import *
from itertools import combinations
import ttk
import time

class ProgBarApp:
    def __init__(self):
        self.vals = range(1, 20)
        self.combs = list(combinations(self.vals,3))
        self.n = len(self.combs)
        self.progressbar = ttk.Progressbar(root, maximum = self.n+1)
        self.progressbar.pack()

    def start(self):
        for c in self.combs:
            self.progressbar.step()
            time.sleep(0.01)    
            root.update()
        root.destroy()

root = Tk()
p = ProgBarApp()
root.after(0, p.start())
root.mainloop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top