【问题标题】:How can i load class in notebook when tab is clicked in wxpython?在 wxpython 中单击选项卡时,如何在笔记本中加载类?
【发布时间】:2020-11-18 16:02:42
【问题描述】:

在这里,我怀疑当时单击笔记本选项卡时,只有它应该在该选项卡中加载类。但是在 wxpython 中,默认情况下所有类都加载到选项卡中,所以我如何在单击选项卡时设置条件来加载类。 这是一个小例子。

import wx

class tabclass(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is the help tab", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="notebook")
        mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
        self.nb = wx.Notebook(mainPanel,size=(1365, 700))
        tab0 = tabclass(self.nb)
        self.nb.AddPage(tab0, "Tab One")

if __name__ == "__main__":
    app = wx.App()  
    MainFrame().Show()
    app.MainLoop()
    

这里我想在单击选项卡时显示静态文本,否则类不应该加载。

【问题讨论】:

  • @mihrbhatt,为什么?事情不是这样运作的。只需加载选项卡布局不需要任何资源。因为只要您调用AddPage(),页面就会添加到笔记本中,并且页面上的每个控件都会创建。你当然可以捕捉到换页事件,但是当用户切换到不同的页面时你会怎么做呢?你只会浪费一切——处理器时间、执行时间和资源
  • 我很感激,但是当我希望 gui 加载得更快时,单击选项卡时应该如何加载类?
  • @mihrbhatt,您不会获得任何加载速度。事实上,您的程序可能会按照您希望它的行为方式减慢速度。

标签: python-3.x wxwidgets wxpython


【解决方案1】:

您可能需要一种不同的方法来选择笔记本中显示的内容和不显示的内容。
例如,菜单似乎是一个合适的选择工具。

import wx

class tabclass(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is the help tab", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="notebook")
        mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
        self.nb = wx.Notebook(mainPanel,size=(1365, 700))

        tabmenu = wx.Menu()
        t1 = wx.MenuItem(tabmenu, id = -1, text="Help", kind=wx.ITEM_CHECK)
        tabmenu.Append(t1)
        t2 = wx.MenuItem(tabmenu, id = -1, text="Other tab", kind=wx.ITEM_CHECK)
        tabmenu.Append(t2)
        t3 = wx.MenuItem(tabmenu, id = -1, text="Another tab", kind=wx.ITEM_CHECK)
        tabmenu.Append(t3)
        tabmenu.Append(wx.ID_EXIT, '&Quit')

        # Creating the menubar.
        menuBar = wx.MenuBar()
        # Add menus
        menuBar.Append(tabmenu, "&Tabs")
        # Adding the MenuBar to the Frame content.
        self.SetMenuBar(menuBar)
        # Bind menu item to functions
        self.Bind(wx.EVT_MENU, self.helptab, t1)
        self.Bind(wx.EVT_MENU, self.othertab, t2)
        self.Bind(wx.EVT_MENU, self.anothertab, t3)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)

    # Obviously, here you would differentiate your tabs
    # I have used the same one for brevity
    def helptab(self, event):
        if event.IsChecked():
            self.tab0 = tabclass(self.nb)
            self.nb.AddPage(self.tab0, "Help Tab")
        else:
            #Delete the "Help Tab"
            pass

    def othertab(self, event):
        if event.IsChecked():
            self.tab1 = tabclass(self.nb)
            self.nb.AddPage(self.tab1, "Other Tab")
        else:
            #Delete the "Other Tab"
            pass

    def anothertab(self, event):
        if event.IsChecked():
            self.tab2 = tabclass(self.nb)
            self.nb.AddPage(self.tab2, "Another Tab")
        else:
            #Delete the "Another Tab"
            pass

    def OnQuit(self, event):
        self.Destroy()

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

【讨论】:

  • 感谢萨克森的@Rolf。我还有一个疑问,我想从 wx 面板中的文件夹中加载数千张图像,那么最好的方法是什么。提前致谢。
  • @mihirbhatt wx.lib.agw.thumbnailctrl 小部件浮现在脑海中,但这是一个完全不同的问题。
  • 的 Saxoy 感谢您的建议。我试过当大约 5000 个图像的缩略图控制滚动条有点滞后。我使用了 WrapSizer()。
  • @mihirbhatt 让我感到惊讶!五千看起来太多了,你不能把图像细分成子目录吗? p.s.使用imagehandler=thumbnailctrl.PILImageHandler 会更快。
猜你喜欢
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多