wxpython - 嵌套笔记本

发布于 2024-12-13 05:29:00 字数 6217 浏览 0 评论 0原文

我一直在努力让我的嵌套笔记本在代码方面更具吸引力。目前,我得到了这个

#!/usr/bin/env python
import os
import sys
import datetime
import numpy as np
from readmonifile import MonitorFile
from sortmonifile import sort
import wx

class NestedPanelOne(wx.Panel):
    #----------------------------------------------------------------------
    # First notebook that creates the tab to select the component number
    #----------------------------------------------------------------------
    def __init__(self, parent, label, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)

        #Loop creating the tabs according to the component name
        nestedNotebook = wx.Notebook(self, wx.ID_ANY)
        for slabel in sorted(data[label].keys()):
            tab = NestedPanelTwo(nestedNotebook, label, slabel, data)
            nestedNotebook.AddPage(tab,slabel)


        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)

        self.SetSizer(sizer)

class NestedPanelTwo(wx.Panel):
    #------------------------------------------------------------------------------
    # Second notebook that creates the tab to select the main monitoring variables 
    #------------------------------------------------------------------------------
    def __init__(self, parent, label, slabel, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)

        nestedNotebook = wx.Notebook(self, wx.ID_ANY)

        for sslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]].keys()):
            tab = NestedPanelThree(nestedNotebook, label, slabel, sslabel, data)
            nestedNotebook.AddPage(tab, sslabel)

        sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)

class NestedPanelThree(wx.Panel):
    #-------------------------------------------------------------------------------
    # Third notebook that creates checkboxes to select the monitoring sub-variables
    #-------------------------------------------------------------------------------
    def __init__(self, parent, label, slabel, sslabel, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        labels=[]
        chbox =[]
        chboxdict={}
        for ssslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]][sslabel].keys()):
            labels.append(ssslabel)

        for item in list(set(labels)):
            cb = wx.CheckBox(self, -1, item)
            chbox.append(cb)
            chboxdict[item]=cb

        gridSizer = wx.GridSizer(np.shape(list(set(labels)))[0],3, 5, 5)

        gridSizer.AddMany(chbox)
        self.SetSizer(gridSizer)

########################################################################
class NestedNotebookDemo(wx.Notebook):
    #---------------------------------------------------------------------------------
    # Main notebook creating tabs for the monitored components
    #---------------------------------------------------------------------------------
    def __init__(self, parent, data):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
                             wx.BK_DEFAULT
                            )

        for label in sorted(data.keys()):
            print label
            tab = NestedPanelOne(self,label, data)
            self.AddPage(tab, label)
########################################################################
class DemoFrame(wx.Frame):
    #----------------------------------------------------------------------
    # Putting it all together
    #----------------------------------------------------------------------
    def __init__(self,data):

        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "pDAQ monitoring plotting tool",
                          size=(800,400)
                          )

        panel = wx.Panel(self)

        notebook = NestedNotebookDemo(panel, data)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()

        #Menu Bar to be added later
        '''
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(1, '&Quit', 'Exit Tool')
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnClose, id=1)
        '''
        self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":

    if len(sys.argv) == 1:
        raise SystemExit("Please specify a file to process")

    for f in sys.argv[1:]:
        data=sort.sorting(f)
        print data['stringHub'].keys()
        print data.keys()
        print data[data.keys()[0]].keys()

    print 'test'
    app = wx.PySimpleApp()
    frame = DemoFrame(data)
    app.MainLoop()
    print 'testend'

,我想将整个混乱减少为只有三个嵌套 for 循环的东西,所以像

for label in sorted(data.keys()):
            self.SubNoteBooks[label] = wx.Notebook(self.Notebook, wx.ID_ANY)
            self.Notebook.AddPage(self.SubNoteBooks[label], label)
            for slabel in sorted(data[label].keys()):
                self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)
                self.SubNoteBooks[label].AddPage(self.SubNoteBooks[label][slabel], slabel)
                for sslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]].keys()):
                    self.SubNoteBooks[label][slabel][sslabel] = wx.Notebook(self.Notebook, wx.ID_ANY)
                    self.Notebook.AddPage(self.SubNoteBooks[label][slabel][sslabel], sslabel)

我一直在尝试摆弄这个问题,但问题似乎是

self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)

我收到错误的行:

Traceback (most recent call last):
  File "./reducelinenumbers.py", line 162, in <module>
    frame = DemoFrame(data)
  File "./reducelinenumbers.py", line 126, in __init__
    self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)
TypeError: 'Notebook' object does not support item assignment

我明白为什么笔记本类型会在这里引发 TypeError 。有办法解决这个问题吗?

提前谢谢大家。

I have been trying to make my nested notebooks a little bit more appealing code wise. At the moment, I got this

#!/usr/bin/env python
import os
import sys
import datetime
import numpy as np
from readmonifile import MonitorFile
from sortmonifile import sort
import wx

class NestedPanelOne(wx.Panel):
    #----------------------------------------------------------------------
    # First notebook that creates the tab to select the component number
    #----------------------------------------------------------------------
    def __init__(self, parent, label, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)

        #Loop creating the tabs according to the component name
        nestedNotebook = wx.Notebook(self, wx.ID_ANY)
        for slabel in sorted(data[label].keys()):
            tab = NestedPanelTwo(nestedNotebook, label, slabel, data)
            nestedNotebook.AddPage(tab,slabel)


        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)

        self.SetSizer(sizer)

class NestedPanelTwo(wx.Panel):
    #------------------------------------------------------------------------------
    # Second notebook that creates the tab to select the main monitoring variables 
    #------------------------------------------------------------------------------
    def __init__(self, parent, label, slabel, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)

        nestedNotebook = wx.Notebook(self, wx.ID_ANY)

        for sslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]].keys()):
            tab = NestedPanelThree(nestedNotebook, label, slabel, sslabel, data)
            nestedNotebook.AddPage(tab, sslabel)

        sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)

class NestedPanelThree(wx.Panel):
    #-------------------------------------------------------------------------------
    # Third notebook that creates checkboxes to select the monitoring sub-variables
    #-------------------------------------------------------------------------------
    def __init__(self, parent, label, slabel, sslabel, data):

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        labels=[]
        chbox =[]
        chboxdict={}
        for ssslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]][sslabel].keys()):
            labels.append(ssslabel)

        for item in list(set(labels)):
            cb = wx.CheckBox(self, -1, item)
            chbox.append(cb)
            chboxdict[item]=cb

        gridSizer = wx.GridSizer(np.shape(list(set(labels)))[0],3, 5, 5)

        gridSizer.AddMany(chbox)
        self.SetSizer(gridSizer)

########################################################################
class NestedNotebookDemo(wx.Notebook):
    #---------------------------------------------------------------------------------
    # Main notebook creating tabs for the monitored components
    #---------------------------------------------------------------------------------
    def __init__(self, parent, data):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
                             wx.BK_DEFAULT
                            )

        for label in sorted(data.keys()):
            print label
            tab = NestedPanelOne(self,label, data)
            self.AddPage(tab, label)
########################################################################
class DemoFrame(wx.Frame):
    #----------------------------------------------------------------------
    # Putting it all together
    #----------------------------------------------------------------------
    def __init__(self,data):

        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "pDAQ monitoring plotting tool",
                          size=(800,400)
                          )

        panel = wx.Panel(self)

        notebook = NestedNotebookDemo(panel, data)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()

        #Menu Bar to be added later
        '''
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(1, '&Quit', 'Exit Tool')
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnClose, id=1)
        '''
        self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":

    if len(sys.argv) == 1:
        raise SystemExit("Please specify a file to process")

    for f in sys.argv[1:]:
        data=sort.sorting(f)
        print data['stringHub'].keys()
        print data.keys()
        print data[data.keys()[0]].keys()

    print 'test'
    app = wx.PySimpleApp()
    frame = DemoFrame(data)
    app.MainLoop()
    print 'testend'

and I would like to reduce this whole mess into something that only has three nested for loops, so something like

for label in sorted(data.keys()):
            self.SubNoteBooks[label] = wx.Notebook(self.Notebook, wx.ID_ANY)
            self.Notebook.AddPage(self.SubNoteBooks[label], label)
            for slabel in sorted(data[label].keys()):
                self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)
                self.SubNoteBooks[label].AddPage(self.SubNoteBooks[label][slabel], slabel)
                for sslabel in sorted(data[label][slabel][data[label][slabel].keys()[0]].keys()):
                    self.SubNoteBooks[label][slabel][sslabel] = wx.Notebook(self.Notebook, wx.ID_ANY)
                    self.Notebook.AddPage(self.SubNoteBooks[label][slabel][sslabel], sslabel)

I have been trying to fiddle this around but the problem seems to be the line

self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)

I get the error:

Traceback (most recent call last):
  File "./reducelinenumbers.py", line 162, in <module>
    frame = DemoFrame(data)
  File "./reducelinenumbers.py", line 126, in __init__
    self.SubNoteBooks[label][slabel] = wx.Notebook(self, wx.ID_ANY)
TypeError: 'Notebook' object does not support item assignment

I understand why notebook is being type raises a TypeError here. Is there a way around this?

Thanks a bunch in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2024-12-20 05:29:00

您正在像字典一样使用笔记本,因此它不起作用。
你可以这样做:

from collections import defaultdict

self.sub_nbks = defaultdict(lambda:defauldict(dict))

for label in sorted(data.keys()):
    self.sub_nbks[label]['top']['top'] = wx.Notebook(self.Notebook, wx.ID_ANY)
    self.Notebook.AddPage(self.sub_nbks[label]['top']['top'], label)
    for slabel in sorted(data[label].keys()):
        self.sub_nbks[label][slabel]['top'] = wx.Notebook(self.Notebook, wx.ID_ANY)
        self.sub_nbks[label]['top']['top'].AddPage(self.sub_nbks[label][slabel]['top'], slabel)
        for sslabel in sorted(data[label][slabel].keys()):
            self.sub_nbks[label][slabel][sslabel] = wx.Notebook(self.Notebook, wx.ID_ANY)
            self.sub_nbks[label][slabel]['top'].AddPage(self.sub_nbks[label][slabel][sslabel], sslabel)

You are using a Notebook like a dictionary, so it doesnt work.
You can do:

from collections import defaultdict

self.sub_nbks = defaultdict(lambda:defauldict(dict))

for label in sorted(data.keys()):
    self.sub_nbks[label]['top']['top'] = wx.Notebook(self.Notebook, wx.ID_ANY)
    self.Notebook.AddPage(self.sub_nbks[label]['top']['top'], label)
    for slabel in sorted(data[label].keys()):
        self.sub_nbks[label][slabel]['top'] = wx.Notebook(self.Notebook, wx.ID_ANY)
        self.sub_nbks[label]['top']['top'].AddPage(self.sub_nbks[label][slabel]['top'], slabel)
        for sslabel in sorted(data[label][slabel].keys()):
            self.sub_nbks[label][slabel][sslabel] = wx.Notebook(self.Notebook, wx.ID_ANY)
            self.sub_nbks[label][slabel]['top'].AddPage(self.sub_nbks[label][slabel][sslabel], sslabel)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文