Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter GUI is freezing

Tags:

python

tkinter

I don't know if I use the right code for do that. I wrote a small script to find a folder in hard disk:

import sys
from tkinter import *
from tkinter import ttk
import threading
import os
mGui = Tk()
mGui.geometry('450x80')
mGui.title('Copy folder')
progressbar = ttk.Progressbar(orient=HORIZONTAL, length=200, mode='determinate')
progressbar.pack(side="bottom")

xe = 'progresscache'
def handle_click():
    progressbar.start()
    def searcher():
        for root, dirs, files in os.walk(r'c:'):
            for name in dirs:
                if name == xe:
                    print ("find !")
                    progressbar.stop()          
    t = threading.Thread(target=searcher)
    t.start()

dirBut = Button(mGui, text='Go find !', command = handle_click)
dirBut.pack()
mGui.mainloop()

After several attempts, I still had to freeze the GUI,When I clicked on my boutton.
So I decided to call the action with a thread.
I do not know if we should do it this way to avoid freeze...

Well, everything seems to work without freeze..

Now, I want to do a class with my code, but every time I get an error for threads Here is my code :


My class Searcher.py (in Appsave folder)

import os
import threading
class Searcher:

    def recherche(zeFolder):
        for root, dirs, files in os.walk(r'c:'):
            for name in dirs:
                if name == zeFolder:
                    print ("Finded !")
                    progressbar.stop()
    threading.Thread(target=recherche).start()

my main .py

# -*- coding: utf-8 -*-
import sys
from tkinter import *
from tkinter import ttk
import threading
import os
from Appsave.Searcher import Searcher

mGui = Tk()
mGui.geometry('450x80')
mGui.title('Djex save')
progressbar = ttk.Progressbar(orient=HORIZONTAL, length=200, mode='determinate')
progressbar.pack(side="bottom")

xe = 'progresscache'
la = Searcher
def handle_click():
    progressbar.start()
    la.recherche(xe) 
dirBut = Button(mGui, text='Go find !', command = handle_click)
dirBut.pack()
mGui.mainloop()

here is the output error

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\python34\lib\threading.py", line 921, in _bootstrap_inner
    self.run()
  File "C:\python34\lib\threading.py", line 869, in run
    self._target(*self._args, **self._kwargs)
TypeError: recherche() missing 1 required positional argument: 'zeFolder'

I hope to have enough detail my problem to find help, thank you

like image 582
jmercier Avatar asked May 07 '26 01:05

jmercier


1 Answers

You should try subclassing Thread, like this:

class Searcher(threading.Thread):

    def __init__(self, zeFolder, progressbar):
        super(Searcher, self).__init__()
        self.zeFolder = zeFolder
        self.progressbar = progressbar

    def run(self):
        for root, dirs, files in os.walk(r'c:'):
            for name in dirs:
                if name == self.zeFolder:
                    print ("Finded !")
                    self.progressbar.stop()

And then, call it like this:

xe = 'progresscache'
la = Searcher(xe, progressbar)
def handle_click():
    progressbar.start()
    la.start()

Instead of:

xe = 'progresscache'
la = Searcher
def handle_click():
    progressbar.start()
    la.recherche(xe) 

Hope it helps!

like image 188
cdonts Avatar answered May 08 '26 14:05

cdonts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!