在wx.Panel中使用wx.Frame中的变量

时间:2014-07-19 12:55:42

标签: python user-interface matplotlib wxpython python-imaging-library

我创建了一个分为两部分的应用程序;左边是具有滑块的框架,我想用这些滑块来修改右侧面板。

以下是代码:

import wx
import numpy 
import matplotlib
import random
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import Image
import ImageEnhance

#from wx.lib.pubsub import pub 

class Main(wx.Frame):

    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(1200,800))
        self.sp = wx.SplitterWindow(self)
        self.varPanel = wx.Panel(self.sp, style=wx.RAISED_BORDER)
        self.imgPanel = ImgPanel(self.sp)
        self.sp.SplitVertically(self.varPanel,self.imgPanel,300)
        self.conSlider = wx.Slider(self.varPanel, -1, 0, 1, 10, pos=(10,10), size=(250,-1), style=wx.SL_AUTOTICKS | wx.SL_LABELS)
        self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
        self.Show(True)

    def sliderUpdate(self, event):
        self.conValue = self.conSlider.GetValue()
        #print "Contrast: ", self.conValue
        return self.conValue
        #pub.sendMessage("panelListener", message=self.conValue)

class ImgPanel(wx.Panel):

    def __init__(self, parent):     
        wx.Panel.__init__(self, parent,-1,size=(50,50))

        #pub.subscribe(self.myListener, "panelListener")

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.fig = plt.figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.fig)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)

        self.drawImg()

    '''def myListener(self, message, arg2=None):
        """
        Listener function
        """
        return message'''

    def contrast(self, img, value):
        enhancer = ImageEnhance.Contrast(img)
        conImg = enhancer.enhance(value)
        return conImg

    def drawImg(self):

        img = Image.open("d50.tif").convert("L")

        #cIm = self.contrast(img, random.uniform(0, 3))
        #cIm = self.contrast(img, message)
        cIm = self.contrast(img, self.conValue) # how to get slider value?? 

        arr = numpy.array(cIm) 
        im  = plt.imshow(arr, cmap=plt.cm.gray)

if __name__ == '__main__':
    app = wx.App()
    frame = Main("Title")
    app.MainLoop()

我希望框架中滑块的值向下传递到面板中的drawImg()方法。我认为这是可能的,因为框架是面板的父级。但是,运行时我得到conValue的属性not found错误。

我尝试使用此处所述的消息http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/,但没有任何运气(我的代码试图将其注释掉)

请有人帮忙解释我做错了什么,以及如何在我的面板中使用滑块变量。

- - - - - - - - 编辑

基于Sundars的新代码帮助:

import wx
import numpy 
import matplotlib
import random
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import Image
import ImageEnhance

#from wx.lib.pubsub import pub 

class Main(wx.Frame):

    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(1200,800))

        self.conValue=''
        self.sp = wx.SplitterWindow(self)
        self.varPanel = wx.Panel(self.sp, style=wx.RAISED_BORDER)
        self.imgPanel = ImgPanel(self, self.sp)
        self.sp.SplitVertically(self.varPanel,self.imgPanel,300)
        self.conSlider = wx.Slider(self.varPanel, -1, 0, 1, 10, pos=(10,10), size=(250,-1), style=wx.SL_AUTOTICKS | wx.SL_LABELS)
        self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
        self.Show(True)
        self.conValue = ""

    def sliderUpdate(self, event):
        self.conValue = self.conSlider.GetValue()
        #print "Contrast: ", self.conValue
        return self.conValue
        #pub.sendMessage("panelListener", message=self.conValue)

class ImgPanel(wx.Panel):

    def __init__(self, frameclass, parent):     
        wx.Panel.__init__(self, parent,-1,size=(50,50))
        self.frameclass=frameclass
        #pub.subscribe(self.myListener, "panelListener")
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.fig = plt.figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.fig)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)

        self.drawImg()

    '''def myListener(self, message, arg2=None):
        """
        Listener function
        """
        return message'''

    def contrast(self, img, value):
        '''
        Method to alter the contrast of an image. 
        A factor of 0.0 gives a solid grey image. A factor of 1.0 gives the original image.
        Returns the altered image.
        '''
        enhancer = ImageEnhance.Contrast(img)
        conImg = enhancer.enhance(value)
        return conImg

    def drawImg(self):

        img = Image.open("d50.tif").convert("L")

        #cIm = self.contrast(img, random.uniform(0, 3)) # contrast change 
        #cIm = self.contrast(img, message)
        cIm = self.contrast(img, self.frameclass.conValue)

        arr = numpy.array(cIm) 
        im  = plt.imshow(arr, cmap=plt.cm.gray)

if __name__ == '__main__':
    app = wx.App()
    frame = Main("Title")
    app.MainLoop()

我现在收到此错误:

Traceback (most recent call last):
  File "C:\Users\Nathan\Documents\Visual Studio 2013\Projects\VSEM\VSEM\module2.
py", line 78, in <module>
    frame = Main("Title")
  File "C:\Users\Nathan\Documents\Visual Studio 2013\Projects\VSEM\VSEM\module2.
py", line 21, in __init__
    self.imgPanel = ImgPanel(self, self.sp)
  File "C:\Users\Nathan\Documents\Visual Studio 2013\Projects\VSEM\VSEM\module2.
py", line 47, in __init__
    self.drawImg()
  File "C:\Users\Nathan\Documents\Visual Studio 2013\Projects\VSEM\VSEM\module2.
py", line 71, in drawImg
    cIm = self.contrast(img, self.frameclass.conValue)
  File "C:\Users\Nathan\Documents\Visual Studio 2013\Projects\VSEM\VSEM\module2.
py", line 62, in contrast
    conImg = enhancer.enhance(value)
  File "C:\Anaconda\lib\site-packages\PIL\ImageEnhance.py", line 36, in enhance
    return Image.blend(self.degenerate, self.image, factor)
  File "C:\Anaconda\lib\site-packages\PIL\Image.py", line 2008, in blend
    return im1._new(core.blend(im1.im, im2.im, alpha))
TypeError: a float is required
Press any key to continue . . .

1 个答案:

答案 0 :(得分:0)

第一件事:

在init中的Frame类中创建变量

self.conValue=''

接下来,在调用imagepanel时也传递Frame类

self.imgPanel = ImgPanel(self,self.sp)

接下来是图像类的初始化

class ImgPanel(wx.Panel):

    def __init__(self,frameclass, parent): 

       self.frameclass=frameclass

现在,您可以将其称为

cIm = self.contrast(img, self.frameclass.conValue)