[英]Tkinter GUI is freezing
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. 经过几次尝试,我仍然不得不冻结GUI,当我点击我的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) 我的班级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()
my main .py 我的主要.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 我希望有足够的细节我的问题找到帮助,谢谢
You should try subclassing Thread
, like this: 您应该尝试子类化
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()
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! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.