2

我正在制作一个经过一些处理后绘制图形的应用程序。这是在用户引入一些值并按下按钮后完成的。但是我没有得到这个数字。下面有一个简化的代码。如果我直接绘制 t 和 s 的值,这可以正常工作,但如果在按下按钮后完成,则不行。我错过了什么?还有其他更好的方法吗?

from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

import wx

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Input variables
        self.button = wx.Button(self, label="Go")

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(1, 1)
        self.sizer.Add(self.button, (1, 2), (3, 6), flag=wx.EXPAND)
        self.SetSizer(self.sizer)

class Output_Panel_Var(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # Output variables
        self.tittle = wx.StaticText(self, label="OUTPUTS:")
        self.font = wx.Font(12, wx.DECORATIVE, wx.BOLD, wx.NORMAL)
        self.tittle.SetFont(self.font)
        self.lblt = wx.StaticText(self, label="t:")
        self.resultt = wx.StaticText(self, label="", size=(100, -1))
        self.lbls = wx.StaticText(self, label="s:")
        self.results = wx.StaticText(self, label="", size=(100, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(2, 2)
        self.sizer.Add(self.tittle, (1, 3))
        self.sizer.Add(self.lblt, (3, 1))
        self.sizer.Add(self.resultt, (3, 2))
        self.sizer.Add(self.lbls, (4, 1))
        self.sizer.Add(self.results, (4, 2))
        self.SetSizer(self.sizer)

class Output_Panel_Fig(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)

    def draw(self,t,s):
        self.axes.plot(t, s)


class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())

        # Set variable panels
        self.main_splitter = wx.SplitterWindow(self)
        self.out_splitter = wx.SplitterWindow(self.main_splitter)
        self.inputpanel = Input_Panel(self.main_splitter)
        self.inputpanel.SetBackgroundColour('#c4c4ff')
        self.outputpanelvar = Output_Panel_Var(self.out_splitter)
        self.outputpanelvar.SetBackgroundColour('#c2f1f5')
        self.outputpanelfig = Output_Panel_Fig(self.out_splitter)
        self.main_splitter.SplitVertically(self.inputpanel, self.out_splitter)
        self.out_splitter.SplitHorizontally(self.outputpanelvar, self.outputpanelfig)

        # Set event handlers
        self.inputpanel.button.Bind(wx.EVT_BUTTON, self.OnButton)      

    def OnButton(self, e):
        t = arange(0.0, 1.0, 0.01)
        s = sin(2 * pi * t)
        #self.outputpanelvar.resultt.SetLabel('%.5f' % t)
        #self.outputpanelvar.resultt.SetLabel('%.5f' % s)
        self.outputpanelfig.draw(t,s)

def main():
    app = wx.App(False)
    frame = Main_Window(None, "T-Matrix Codes GUI")
    frame.Show()
    app.MainLoop()
if __name__ == "__main__" :
    main()
4

1 回答 1

1

我认为您缺少画布的重绘。仅绘制新绘图是不够的,还必须刷新绘图窗格!在 Output_Panel_Fig 的 draw 方法中的 plot 命令之后添加一个 self.canvas.draw() 这应该会有所帮助。

import ...

class Input_Panel(wx.Panel):
    def __init__(self, parent):
        ...

class Output_Panel_Var(wx.Panel):
    def __init__(self, parent):
        ...

class Output_Panel_Fig(wx.Panel):
    def __init__(self, parent):
        ...

    def draw(self,t,s):
        self.axes.plot(t, s)

        self.canvas.draw()


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

    def OnButton(self, e):
        ...

def main():
    ...

if __name__ == "__main__" :
    main()
于 2013-10-21T06:17:34.450 回答