2

I have developed an App to copy files on disk and display its progress in Tkinter GUI. It uses customized shutil module for copying files. If I use my customized copy function directly (without GUI) to copy files, then it copy all the files properly and display message for file being copied (have used print statement for the being copied). Now the problem is when I use the GUI for performing same operation, it hung after some time.

I am using Text Widget to display the running log of the files being copied. Basically I have re-directed the stdout before calling my copy function and I am using “update_idletasks” to update the GUI.

class TextRedirector(object):
    def __init__(self, widget, tag="stdout"):
        self.widget = widget
        self.tag = tag

    def write(self, str):
         self.widget.configure(state="normal")
         self.widget.insert("end", str, (self.tag,))
         self.widget.update_idletasks()
         self.widget.see('end')
         self.widget.configure(state="disabled")

Query 1: Is there any limit on the text size in Text widget? What should I look in my code to address this hang issue.

Query2 : I have noticed that once the copy function is called, I am not able to use “Minimize” button, I can only use it once this operation is over. In my case, I need to copy huge data, so I want to minimize the App and continue with my other work.

UPDATE: (WORKING SOLUTION)

Both of my queries are addressed if i use update method instead of update_idletasks. Now my question why it didn't worked with update_idletasks. As per my knowledge it is also used to refresh the GUI events.

class TextRedirector(object):
    def __init__(self, widget, tag="stdout"):
        self.widget = widget
        self.tag = tag

def write(self, str):
     self.widget.configure(state="normal")
     self.widget.insert("end", str, (self.tag,))
     self.widget.update()
     self.widget.see('end')
     self.widget.configure(state="disabled")
1
  • 1
    I had same problem when Close Button wasn't working inside a 10 second loop that updated idle tasks every .1 second so it should work. Using .update() instead of .update_idletasks() allowed the button to work (along with ESCAPE and WM_DELETE). Mar 4, 2021 at 20:08

1 Answer 1

1

There is no practical size limitation in the text widget.

Without seeing how you're actually copying the data it's impossible to know for sure, but are you aware that Tkinter is single threaded? If you have a command that takes a long time, the GUI will hang until that operation completes. This is because all GUI operations happen by responding to events, and while any individual command is running the event loop can't respond to events.

The workarounds are to have that long running operation run in a thread or a separate process. Or, you can refactor that function so that small chunks of work can be done in each iteration of the event loop. Be aware that if you use threads, you cannot directly write to the GUI widgets from this other thread. You have to use a thread safe queue to send data between the threads.

4
  • Well function which i call copies files from disk, so it may take time (depending on data size, but my data is already divided into smaller files of size no longer than 15 mb). That's why i have used update idletasks to update GUI after copying one file. I noticed that after copying few files, it stops responding and after some time it shows the remaining files status (by that time all other remaining files were copied)
    – sarbjit
    Sep 11, 2012 at 12:18
  • Can you please point me some reference(example) for threading solution?
    – sarbjit
    Sep 11, 2012 at 12:52
  • My problem is fixed if i use "update" instead of "update_idletasks". Now question is why it was not working with update_idletasks and working with update method
    – sarbjit
    Sep 11, 2012 at 14:23
  • @sarbjit You should post an answer of what made it work. This might help others who are using .update_idletasks(). I lost hours myself today maintaining old code that stopped working probably a year ago... Mar 4, 2021 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.