如何优化wxpython GUI(最好没有线程)?代码提示如下?

时间:2012-05-03 09:17:20

标签: multithreading user-interface wxpython

我的GUI与下面的代码没有线程。图像显示占用大量内存,GUI被阻止,我一次只能调用一个函数。请建议简单的黑客使GUI更快。不管怎样的图像像聚类这样的处理任务需要5-6分钟。

import wx
import sys
import os
import matplotlib
import OpenGL
import PIL
import time
from spectral.graphics.hypercube import hypercube
from spectral import *
init_graphics()

class RedirectText(object):
 def __init__(self,awxTextCtrl):
    self.out=awxTextCtrl

def write(self,string):
    self.out.WriteText(string)            


 class Frame(wx.Frame):
def __init__(self, title,*args,**kwargs):
    wx.Frame.__init__(self, None, title=title, size=       (1000,85),style=wx.MINIMIZE_BOX|wx.CLOSE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.CAPTION|wx.CLIP_CHILDREN)
    self.Bind(wx.EVT_CLOSE, self.OnClose)
    panel=wx.Panel(self,-1)
    self.button=wx.Button(panel,label="Open",pos=(0,0),size=(50,30))
    self.button1=wx.Button(panel,label="Save",pos=(51,0),size=(50,30))
    self.button2=wx.Button(panel,label="ROI",pos=(102,0),size=(50,30))
    self.button3=wx.Button(panel,label="Tone",pos=(153,0),size=(50,30))
    self.slider=wx.Slider(panel,pos=(204,0))
    self.button4=wx.Button(panel,label="Header",pos=(305,0),size=(50,30))
    self.button5=wx.Button(panel,label="Cluster",pos=(356,0),size=(50,30))
    self.button6=wx.Button(panel,label="Cube",pos=(407,0),size=(50,30))
    self.button7=wx.Button(panel,label="Gaussian",pos=(458,0),size=(50,30))
    self.Bind(wx.EVT_BUTTON, self.OnCubeClick,self.button5)
    self.Bind(wx.EVT_BUTTON, self.OnHeadClick,self.button4)
    self.Bind(wx.EVT_BUTTON, self.OnSaveClick,self.button1)
    self.Bind(wx.EVT_BUTTON, self.OnButtonClick,self.button)
    self.Bind(wx.EVT_BUTTON, self.OnCClick,self.button6)
    self.Bind(wx.EVT_BUTTON, self.OnGClick,self.button7)
    #self.std=wx.TextCtrl(panel,pos=(0,31), size=(500,-1))
    self.loc=wx.TextCtrl(panel,pos=(700,0), size=(300,-1))
    self.status = wx.TextCtrl(panel,-1,'Choose file',pos=(800,22),size=(200,-1))
    #redir=RedirectText(self.std)
    #sys.stdout=redir


def OnButtonClick(self,event):
    wild="HSi Files|*.lan*|All Files|*.*"
    dlg=wx.FileDialog(self,message="Choose a File",wildcard=wild,style=wx.FD_OPEN)
    if dlg.ShowModal() == wx.ID_OK:
        time.sleep(0.5)
        self.loc.SetValue(dlg.GetPath())
        dlg.Destroy()
        self.Onview()

def Onview(self):
    filepath=self.loc.GetValue()
    img=image(filepath)
    time.sleep(1)
    view(img)
    time.sleep(1)
    self.status.SetValue('View Ready')



def OnHeadClick(self,event):

    filepath=self.loc.GetValue()
    img=image(filepath)
    self.status.SetValue(img.shape)

def OnCubeClick(self,event):
    time.sleep(0.2)
    self.status.SetValue('Clustering')
    filepath=self.loc.GetValue()
    img= image(filepath).load()
    (m, c) = cluster(img, 20)
    view_indexed(m)
    self.status.SetValue('Clustering View Ready')

def OnCClick(self,event):
    self.status.SetValue('Cube view')
    time.sleep(5)
    filepath=self.loc.GetValue()
    img= image(filepath).load()
    hypercube(img, bands=[29, 19, 9])

def OnGClick(self,event):
    self.status.SetValue('Gaussian procesing')
    time.sleep(30)
    filepath=self.loc.GetValue()
    gt=image(filepath).read_band(0)
    img=image(filepath)
    classes = create_training_classes(img,gt)
    gmlc = GaussianClassifier(classes)
    clMap = gmlc.classify_image(img)
    view_indexed(clMap)
    self.status.SetLabel('Gaussian Ready')


def OnSaveClick(self,event):
    self.status.SetValue('Save File')
    wild="HSi Files|*.lan*|All Files|*.*"
    dlg=wx.FileDialog(self,message="Save AS",wildcard=wild,style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
    if dlg.ShowModal() == wx.ID_OK:
        path=dlg.GetPath()
        self.Save(path)
        self.file=path
        dlg.Destroy()


def OnClose(self, event):
    dlg = wx.MessageDialog(self, 
        "Do you really want to close BBvw ?",
        "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
    result = dlg.ShowModal()
    dlg.Destroy()
    if result == wx.ID_OK:
        self.Destroy()




  app = wx.App(redirect=False)
  top = Frame("BBvw")
  top.Show()
  app.MainLoop()

1 个答案:

答案 0 :(得分:1)

长时间运行的进程应始终进入单独的线程。否则他们将阻止GUI的主循环。如果显示图像占用大量内存,那么我猜你正在以全分辨率显示高分辨率的照片。尝试创建一组较低分辨率的照片进行显示(如缩略图或其他内容)。除非您需要显示完整分辨率,否则请不要这样做。