Seite 1 von 1

Problem mit wx.Notebook

Verfasst: Freitag 22. Januar 2010, 19:49
von sebastian06
Hallo,

ich habe eine Frage zu wx.Notebook. Hier meine Klasse MplPanel:

Code: Alles auswählen

class MplPanel(wx.Panel):
    def __init__(self, parent, *args, **kwds):
        wx.Panel.__init__(self, parent, *args, **kwds)
        ...
        self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        self.toolbar.Realize()
        self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.toolbar.Show()
        self.SetSizer(self.sizer)
        self.Fit()
Und dann habe ich noch meinen Haupt-Frame:

Code: Alles auswählen

class MplFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.MAXIMIZE|wx.MAXIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER|wx.FULL_REPAINT_ON_RESIZE|wx.CLIP_CHILDREN
        wx.Frame.__init__(self, *args, **kwds)

        self.createMenuBar()
        self.MplFrame_statusbar = self.CreateStatusBar(1, 0)
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        page_graph = MplPanel(self.nb)
        page_abs = Page_Abs(self.nb)
        page_flu = Page_Flu(self.nb)

        # add the pages to the notebook with the label to show on the tab
        self.nb.AddPage(page_graph, "Graph")
        self.nb.AddPage(page_abs, "Abs")
        self.nb.AddPage(page_flu, "Flu")

        self.SetTitle("Test")
        self.SetSize((1000, 700))
        self.MplFrame_statusbar.SetStatusWidths([-1])
        MplFrame_statusbar_fields = ["MplFrame_statusbar"]
        for i in range(len(MplFrame_statusbar_fields)):
            self.MplFrame_statusbar.SetStatusText(MplFrame_statusbar_fields[i], i)
        
        mplsizer1 = wx.BoxSizer(wx.VERTICAL)
        mplsizer1.Add(self.nb, 1, wx.ALL|wx.EXPAND, 0)
        self.SetSizer(mplsizer1)
        self.Layout()
...
    def OnLoadAbs(self, event):
        dlg = wx.FileDialog(self, "Choose Dye Absorption Spectra", os.getcwd(), "", "*.abs*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            pathabs = dlg.GetPath()
            mypathabs = os.path.basename(pathabs)
            self.SetStatusText("Selected Absorbtion Spectra: %s" % mypathabs)
            abs = np.loadtxt(pathabs)
            abs = normspec(abs)
            #self.gabs, = self.mplpanel.axes.plot(abs[:,0],abs[:,1],'k-', lw=2,label = mypathabs)
            self.gabs, = self.nb.page_graph.axes.plot(abs[:,0],abs[:,1],'k-', lw=2,label = mypathabs)
            AdjustDisplay(self)
            self.ToggleItem(event, 2)
            if self.DisplayData == True:
                fc = "black"
                self.ShowSpectralData(event,  pathabs,  abs, fc)
            
        dlg.Destroy()
Und jetzt kommt das Problem. Wenn ich auf der Seite mit dem Graph (page_graph), meinen Plot machen will, bekomme ich das nicht hin. Die auskommentierte Zeile 40 ist aus einer älteren Programmversion (ohne wx.Notebook). Und Zeile 41 funktioniert nicht. Fehelrmeldung lautet: Unhandled Attribute Error: 'Notebook' object attribute has no attribute 'page_graph'.

Ich habe schon viel probiert, aber ich glaube ich mache da einen grundsätzlichen Fehler, nur welcher ist es?

Danke für Eure Tipps.

Grüße,

Sebi

Verfasst: Freitag 22. Januar 2010, 20:36
von Dav1d
Jetzt frei nach Gedächtnis, du musst den Rückgabewert von

Code: Alles auswählen

self.nb.AddPage(page_graph, "Graph")
"speichern" z.B.

Code: Alles auswählen

self.nb_graph = self.nb.AddPage(page_graph, "Graph")
und dann

Code: Alles auswählen

self.nb_graph.axes.plot(abs[:,0],abs[:,1],'k-', lw=2,label = mypathabs)
Ansonsten schau die das mal an: http://www.wxpython.org/docs/api/wx.Boo ... class.html (wx.Notebook erbt davon!)

Verfasst: Samstag 23. Januar 2010, 20:12
von sebastian06
Serus Dav1d,

ich habe Deine Rat befolgt, aber die Zeile:

Code: Alles auswählen

self.nb_graph = self.nb.AddPage(page_graph, "Graph")
liefert einen Boolean als Rückgabe, in diesen Fall ist dann self.nb_graph = True. Und das hilft mir leider nicht so recht weiter ...

Verfasst: Samstag 23. Januar 2010, 21:34
von Dav1d
ok sry,

mir sind die Ideen ausgegangen, ich hab viel probiert in der wxPython Demo, das einzigste was du machen könntest die Seite über ein Event bekommen, oder das wx.aui.Notebook benutzten, dort würde es ein GetPageIndex(page) geben

Verfasst: Samstag 23. Januar 2010, 23:26
von BlackJack
Wie wär's denn einfach `page_graph` an das `Frame`-Exemplar zu binden, bevor man das Objekt zum Notebook hinzufügt!?

Verfasst: Dienstag 26. Januar 2010, 14:22
von sebastian06
Hallo BlackJack,

Hurra! Dein Tip hat mein Problem gelöst

Code: Alles auswählen

class MplFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.MAXIMIZE|wx.MAXIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER|wx.FULL_REPAINT_ON_RESIZE|wx.CLIP_CHILDREN
        wx.Frame.__init__(self, *args, **kwds)

        self.createMenuBar()
        self.MplFrame_statusbar = self.CreateStatusBar(1, 0)
        p = wx.Panel(self)
        nb = wx.Notebook(p)
        self.page_graph = MplPanel(nb)
        self.page_abs = Page_Abs(nb)
        self.page_flu = Page_Flu(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page_graph, "Graph")
        nb.AddPage(page_abs, "Abs")
        nb.AddPage(page_flu, "Flu")
...
def OnLoadAbs(self, event):
        dlg = wx.FileDialog(self, "Choose Dye Absorption Spectra", os.getcwd(), "", "*.abs*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            pathabs = dlg.GetPath()
            mypathabs = os.path.basename(pathabs)
            self.SetStatusText("Selected Absorbtion Spectra: %s" % mypathabs)
            abs = np.loadtxt(pathabs)
            abs = normspec(abs)
            #self.gabs, = self.mplpanel.axes.plot(abs[:,0],abs[:,1],'k-', lw=2,label = mypathabs)
            self.gabs, = self.page_graph.axes.plot(abs[:,0],abs[:,1],'k-', lw=2,label = mypathabs)
...
Vieln Dank nochmal!

Sebi