Tkinter GUI冻结

谢谢

我不知道我是否使用正确的代码来做到这一点。我写了一个小脚本来在硬盘上找到一个文件夹:

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()

经过几次尝试,当我单击按钮时,我仍然不得不冻结GUI。
因此,我决定使用线程调用该动作。
我不知道我们是否应该这样避免冻结...

嗯,一切似乎都在不冻结的情况下进行。

现在,我想用我的代码做一个类,但是每次遇到线程错误时,这是​​我的代码:


我的课程Searcher.py(在Appsave文件夹中)

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()

我的主要.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()

这是输出错误

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'

我希望我的问题有足够的细节来寻求帮助,谢谢

点子

您应该尝试子类化Thread,如下所示:

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()

然后,这样称呼它:

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

代替:

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

希望能帮助到你!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章