I have a script that makes a backup copy of the folders I need. I made a GUI for it using tkinter. The whole script and its GUI worked fine and without any complaints even with large files of 2Giga or more.
I decided to add ttk.Progressbar so that the script's process itself could be seen (purely for natural reasons).
Here is the progressbar itself and its appearance when after calling it there is no further function. In this case, everything works, but the script is no longer executed:
def progress_bar (self):
try:
self.frame7.destroy ()
if self.frame7.winfo_exists ()== 0:
self.progress= ttk.Progressbar (self.frame8, orient= HORIZONTAL,
length= 300, mode= 'indeterminate')
self.progress.grid (columnspan= 20, padx= 40)
self.progress.start ()
except Exception as e:
print (str (e))
But if, after starting the progress bar itself, add a script execution function to archive data, then frame7 does not disappear but hangs and ttk.Progressbar does not appear.
def progress_bar (self):
try:
self.frame7.destroy ()
if self.frame7.winfo_exists ()== 0:
self.progress= ttk.Progressbar (self.frame8, orient= HORIZONTAL,
length= 300, mode= 'indeterminate')
self.progress.grid (columnspan= 20, padx= 40)
self.progress.start ()
self.entering_of_changes ()
except Exception as e:
print (str (e))
def entering_of_changes (self):
comment2= self.comments
if os.path.exists (self.remarks):
self.remark= open (homedir + os.sep + remarkfile, 'a')
x= homedir + os.sep + remarkfile
self.remark.write (
time.strftime ('% d% m% Y') +
"-" + time.strftime ('% H% M% S') + "-" + comment2 + "\ n"
)
self.source.append (x)
self.remark.close ()
self.archive ()
else:
self.remark= open (homedir + os.sep + remarkfile, 'a')
x= homedir + os.sep + remarkfile
self.remark.write (
time.strftime ('% d% m% Y') +
"-" + time.strftime ('% H% M% S') + "-" + comment2 + "\ n"
)
self.source.append (x)
self.remark.close ()
self.archive ()
After the script is executed to the end, self.frame7 disappears and a message about the successful execution of the script is displayed. I suppose that the program just hangs during execution and because of this the widget I need does not disappear. Tell me what could be the problem?
In fact, on the contrary, it is not the program that freezes due to the disappearance of the widget, but the window freezes due to the fact that some long operation is being performed in the main thread, and the execution does not return to mainloop for a long time.
insolor2022-01-11 12:45:15thanks @insolor. I will try. Yes, there is a process of archiving for 40 seconds in the self.archive () function.
Neo Matrix2022-01-11 13:50:06@insolor followed your advice and put the function on a separate thread and everything works. x= threading.Thread (target= self.entering_of_changes)
Neo Matrix2022-01-11 14:04:31- python : How to create a large text input field?
- python : How to keep the scale of buttons, text, etc in tkinter?
- python : how to find out the size of a Tkinter window?
- python : Opening photo for background doesn't work
- Python Tkinter not seeing 'TimePL' instance of Label widget
- python : How to create a window in the game for example (dota2)
- python : Error with matrix multiplication. Numpy
- Can't uninstall/install Python
- python : Why does list.remove() incorrectly remove elements in a loop?
- Loading bar in Python 3.x
Well, apparently, somewhere in the functions called from entering_of_changes, some long-term operations are performed that block the work of the main thread (I suspect that in the archive method). Most likely, you will have to take them out into a separate thread or process.
insolor2022-01-11 11:51:38