我对wxpython的textctrl和线程有任何问题。非常感谢您解决此问题的任何帮助。
我的程序处理文件,当处理每个文件时,它在textctrl中列为完成。使用几个文件时,textctrl会响应并立即显示,并且不会消失。即使这些文件很大。对700mb文件和textctrl的测试是否完美。
在处理许多文件时会出现问题,例如20+以上。在这些情况下,textctrl消失6或7秒,然后重新出现并正常工作。
我尝试过正常的线程,守护程序线程等。还尝试使用.join()使事情变得更糟。我想知道这是不是因为我的程序非常耗费处理器,或者我只是做错了什么。
我的线程代码如下所示。到目前为止,这是迄今为止最快的方法,对我的目的来说还不够好。先谢谢,克林顿。
def Worker(self, e, _file):
match = ''
with open(_file, 'r') as f:
data = f.read()
for char in data:
if char in self.key:
match += chr(self.key.index(char))
open(_file, 'w').close()
with open(_file, 'w') as f:
f.write(match)
wx.CallAfter(self.ListFilesEncrypt, e, _file)
if __name__ == '__main__':
for _file in self.file2process:
self.filenum += 1
Thread(target=self.Worker, args=(e, _file,)).start()
答案 0 :(得分:3)
使用线程安全方法更新GUI。在wxPython中,有3个:
您还应该查看wxPython wiki以获取有关wxPython和线程的信息:
我还写了一个关于这个主题的教程:
更新:这是一个创建40个线程和“进程”40组成文件的简单示例。每个线程完成后,它会更新显示。但是,我没有看到你说的问题。
import random
import time
import wx
from threading import Thread
from wx.lib.pubsub import Publisher
########################################################################
class TestThread(Thread):
"""Test Worker Thread Class."""
#----------------------------------------------------------------------
def __init__(self, fname, sleepAmt):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.fname = fname
self.sleepAmt = sleepAmt
self.start() # start the thread
#----------------------------------------------------------------------
def run(self):
"""Run Worker Thread."""
# This is the code executing in the new thread.
time.sleep(self.sleepAmt)
msg = "%s finished in %s seconds!" % (self.fname, self.sleepAmt)
wx.CallAfter(Publisher().sendMessage, "update", msg)
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
panel = wx.Panel(self, wx.ID_ANY)
self.updateText = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
self.btn = btn = wx.Button(panel, label="Start Thread")
btn.Bind(wx.EVT_BUTTON, self.onButton)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.updateText, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
# create a pubsub receiver
Publisher().subscribe(self.updateDisplay, "update")
#----------------------------------------------------------------------
def onButton(self, event):
"""
Runs the thread
"""
for i in range(40):
fname = "test%s.txt" % i
secs = random.choice(range(3, 15))
TestThread(fname, secs)
#----------------------------------------------------------------------
def updateDisplay(self, msg):
"""
Receives data from thread and updates the display
"""
data = msg.data + "\n"
self.updateText.WriteText(data)
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
我使用带有wxPython 2.8.12.1的Python 2.6在Windows 7上运行
答案 1 :(得分:0)
您的主题 不 直接更新文本控件 - 他们应该使用wx.CallAfter
,或者更好的是为主要设置标志(GIU) ,线程更新控件 - 听起来像一个端到端的线程方法可能是合适的。
答案 2 :(得分:0)
超过CPU密集型,可能是IO密集型的。 IO密集型应用程序会对性能产生很大影响,尤其是当您将IO单元用于其他关键目的(如分页)时。我建议一次服5到10个,其余部分排队。