0

I have two functions within my PyQt code, one for the user to pick a file path and put the file path into a text editor box. This works fine. The second function looks in that directory, finds all the files I want and sorts them for me.

However, within this function i find the number of files and the file index its iterating over to calculate a percentage. I want to use this changing percentage variable to increment a progress bar in my UI while the function is iterating over the files.

My problem is that I cannot get this value out of the function while its iterating as it only returns the value while its finished. resulting in my progress bar not functioning correctly. Currently im just trying to get the text editor to display the value of percent to see if Its updating at all.

Defining the "percent" variable as global doesn't work. Am I approaching this wrong?

The function within my UI code called by button 2:

    def file_search(self):
        get = self.textEdit.toPlainText()
        T.sleep(2)
        search(str(get))  
        self.textEdit_2.setText(percent)

The function within called "search" the UI calls when I press button 2:

global percent
def search(file_path):
    print "File Path: " + str(file_path)
    #find all dicom files
    files = glob.glob(str(file_path) + "\*.dcm")

    value = 0
    total = len(files)

    for filename in files:
        print filename
        value = value + 1
        percent = (value/float(total))*100
        print percent

I have taken out the GUI code as it was far too long for clarity sake. If it's needed I can amend this asap.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Ciaran
  • 478
  • 1
  • 8
  • 23
  • Would putting the line "self.progressBar.setProperty("value", percent)" in my search function work? I tried it but it doesn't like the line outside my UI class. – Ciaran Jun 23 '15 at 13:37
  • You mention both Tkinter and PyQT, but you can't use those two together. Which one is the question about? – Bryan Oakley Jun 23 '15 at 14:36
  • PyQt. If i have a variable changing in a function that a button calls. How do I update a progress bar with the value of the variable as the function runs? At the moment, the function runs and when its over it spits out 100% before moving onto updating the progress bar. – Ciaran Jun 23 '15 at 15:13
  • 2
    If the question is about PyQt, why does the code show Tkinter, and why is the question tagged with Tkinter? Can you clarify your question, please? – Bryan Oakley Jun 23 '15 at 16:01
  • Sorry for being so vague! I used QT designer to design the GUI. And added a function to open a file dialog with tkinter when a button is clicked on the GUI (as that's the what Im more familiar with). The Tkinter bit is not relevent, i just wanted to demonstrate that I can use the buttons etc on my GUI. – Ciaran Jun 24 '15 at 09:28
  • What Im looking to achieve is when button 2 is clicked, it runs a function that looks at each file in a folder. lets say it loops over 100 files, so each file that is analysed in this function should update the progress bar 1%. However, the funtion only returns the percentage variable when it has finished looping over the files. Is there a way to add "self.progressBar.setProperty("value", percent)" within the function? (such that it updates the progress bar while looping) – Ciaran Jun 24 '15 at 09:28
  • I think the answer is here, I am currently looking through it... http://stackoverflow.com/questions/20873259/pyqt-how-to-dynamically-update-widget-property-on-outer-variable-value-change – Ciaran Jun 24 '15 at 11:09
  • 1
    You might also want to check out http://stackoverflow.com/q/30823863/1994235 and http://stackoverflow.com/q/29916377/1994235 – three_pineapples Jun 24 '15 at 11:12

0 Answers0