2015-02-08 158 views
3

我不知道我是否使用正确的代码来做到这一点。我写了一个小脚本来查找文件夹中的硬盘: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,当我点击我的boutton。
所以我决定用线程调用这个动作。
我不知道我们是否应该这样做,这样既避免冻...

好了,一切似乎没有冻结工作..

现在,我想做一类我的代码, 但每次我得到线程 这里的错误是我的代码:


我班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' 

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

回答

4

你应该尝试的子类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) 

希望它能帮助!

+0

你好,非常感谢你的支持者。当我建立项目,一切正常,但是当我点击按钮时,我有一个错误:文件“S:\ pypy \ snake \ Appsave \ Searcher.py”,第14行,运行 如果名称== zeFolder: NameError:名字'zeFolder'没有定义 – jmercier 2015-02-08 18:44:16

+1

@jmercier糟糕!在'zeFolder'之前,我忘了把'self.'。检查更新的代码! – cdonts 2015-02-08 19:01:24

+0

你是我的英雄!工作正常 !现在我必须了解你的代码,不要愚蠢地复制粘贴,ty最好的问候 – jmercier 2015-02-08 19:04:58