python - 线程不工作

标签 python wxpython python-multithreading

我试图让这个小文件复制应用程序工作,它显示一个进度条,但我不明白为什么这不起作用,因为它在更新仪表时锁定了 GUI。

import shutil
import os
import threading
import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.source = os.path.expanduser("~/Desktop/FolderToCopy")
        self.destination = os.path.expanduser("~/Desktop/BackupFolder/Temp")

        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        hbox4 = wx.BoxSizer(wx.HORIZONTAL)

        self.getSourceSize = self.get_size(self.source)

        self.gauge = wx.Gauge(panel, -1, self.getSourceSize, size=(150, 25))
        self.btn1 = wx.Button(panel, wx.ID_OK)
        self.abortButton = wx.Button(panel, label="Abort")

        self.Bind(wx.EVT_BUTTON, self.OnButtonSelect, self.btn1)
        self.abortButton.Bind(wx.EVT_BUTTON, self.OnAbortButton, self.abortButton)

        hbox1.Add(self.gauge, 1, wx.ALIGN_CENTRE)
        hbox2.Add(self.btn1, 1, wx.RIGHT, 10)
        hbox4.Add(self.abortButton, 1, wx.RIGHT, 20)
        vbox.Add((0, 50), 0)
        vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
        vbox.Add((0, 30), 0)
        vbox.Add(hbox2, 1, wx.ALIGN_CENTRE)
        vbox.Add(hbox4, 1, wx.ALIGN_CENTRE)
        panel.SetSizer(vbox)
        self.Centre()

    def OnAbortButton(self, e):
        self.shouldAbort = True

    def get_size(self, start_path):
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                total_size += os.path.getsize(fp)
        total_size = total_size / 50
        return total_size

    def OnButtonSelect(self, event):
        thread1 = threading.Thread(target=shutil.copytree, args=(self.source, self.destination))
        thread1.start()
        self.thread1 = threading.Thread(target=self.OnGo(self))
        self.thread1.start()

    def OnCopy(self):
        shutil.copytree(self.source, self.destination)

    def OnGo(self, event):
        self.shouldAbort = False
        getDestinationSize = 0
        get_size = self.get_size
        while getDestinationSize < self.getSourceSize:
            getDestinationSize = get_size(self.destination)
            self.gauge.SetValue(getDestinationSize)
            if self.shouldAbort:
                break

app = wx.App(0)
frame = MyFrame(None, -1, 'gauge.py')
frame.Show(True)
app.MainLoop()

最佳答案

    self.thread1 = threading.Thread(target=self.OnGo(self))
    self.thread1.start()

您正在该行上执行 self.onGo,甚至在线程启动之前。目标的参数应通过 args 传递。由于 OnGo 从未实际使用其 event 参数,因此您只需传入 None 即可。

    self.thread1 = threading.Thread(target=self.OnGo, args=(None,))
    self.thread1.start()

关于python - 线程不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22353923/

相关文章:

python - 在 Python 中引用另一个枚举作为枚举值?

python - 你怎么能用python从mysql写一个字典查找到一个csv

python - wxPython检查c++部分是否被删除

python - 线程未与 ThreadPoolExecutor 并行执行 python

python - Node.js 的 Shlex 拆分等效项

python - wxpython 中 EVT_BUTTON 的按钮标签更改?

python - wxPython 对齐小部件和拟合

Python Multiprocessing 帮助按条件退出

python-3.x - PyQt5 中的多线程

python - 使用 python 从网页中提取 Disqus 评论、声誉和喜欢?