新的 wxNotebook-Pages 在 wxPython 中保持为空

发布于 2024-10-13 15:45:16 字数 1390 浏览 5 评论 0原文

我制作了一些示例代码来尝试 wxNotebook,但是新插入的页面仍然完全是空的。 如果我不使用计时器或新线程,而是在 MainLoop 之前插入页面,则它可以工作。 我怀疑我需要更新、刷新或类似的东西,但我无法让它工作。 我使用 Windows 7 和 Python 2.7。

import wx
import threading

class addNewPages(threading.Thread):  
    def __init__(self, nb):
        super(addNewPages, self).__init__()
        self.nb = nb
    def run(self):
        self.p = 1
        for i in range(1, 5, 1):
            threading.Timer(i, self.adp).start()
    def adp(self):
        self.newpage = Page(self.nb)
        self.newpage.SetBackgroundColour(wx.GREEN)
        wx.StaticText(self.newpage, -1, "PAGE "+str(self.p), style=wx.ALIGN_CENTRE)
        wx.CallAfter(self.nb.AddPage, self.newpage, "Page "+str(self.p))
        self.p += 1

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    th = addNewPages(mf.nb)
    th.start()
    app.MainLoop()

I made some sample code to try wxNotebook, but the new inserted pages remain completely empty.
If I don't use a Timer or a new Thread, but insert the pages before the MainLoop, it works.
I suspect I need an Update, Refresh or something like that anywhere, but I couldn't get it to work.
I use Windows 7 and Python 2.7.

import wx
import threading

class addNewPages(threading.Thread):  
    def __init__(self, nb):
        super(addNewPages, self).__init__()
        self.nb = nb
    def run(self):
        self.p = 1
        for i in range(1, 5, 1):
            threading.Timer(i, self.adp).start()
    def adp(self):
        self.newpage = Page(self.nb)
        self.newpage.SetBackgroundColour(wx.GREEN)
        wx.StaticText(self.newpage, -1, "PAGE "+str(self.p), style=wx.ALIGN_CENTRE)
        wx.CallAfter(self.nb.AddPage, self.newpage, "Page "+str(self.p))
        self.p += 1

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    th = addNewPages(mf.nb)
    th.start()
    app.MainLoop()

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

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

发布评论

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

评论(3

玩世 2024-10-20 15:45:16

这似乎是线程问题,而不是笔记本问题。按如下方式修改代码将创建您期望的表单。我最好的猜测是这是一个范围问题,但我不确定。

import wx

def run(nb):
    for i in range(1, 5, 1):
        adp(nb, i)

def adp(nb, p):
    newpage = Page(nb)
    newpage.SetBackgroundColour(wx.GREEN)
    wx.StaticText(newpage, -1, "PAGE "+str(p), style=wx.ALIGN_CENTRE)
    wx.CallAfter(nb.AddPage, newpage, "Page "+str(p))

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    run(mf.nb)
    app.MainLoop()

This appears to be a threading problem, not a notebook problem. Modifying your code as follows creates the form that you're expecting. My best guess is that this is a scope issue, but I don't know for certain.

import wx

def run(nb):
    for i in range(1, 5, 1):
        adp(nb, i)

def adp(nb, p):
    newpage = Page(nb)
    newpage.SetBackgroundColour(wx.GREEN)
    wx.StaticText(newpage, -1, "PAGE "+str(p), style=wx.ALIGN_CENTRE)
    wx.CallAfter(nb.AddPage, newpage, "Page "+str(p))

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    run(mf.nb)
    app.MainLoop()
葬花如无物 2024-10-20 15:45:16

我不知道这是否对您有帮助,但这是一个愚蠢的演示:

import random
import wx

########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "self.notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tabNum = 3

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")
        btn = wx.Button(panel, label="Add Page")
        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL, 5)

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def onButton(self, event):
        """"""
        tab = TabPanel(self.notebook)
        self.notebook.AddPage(tab, "Tab %s" % self.tabNum)
        self.tabNum += 1

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

I don't know if this will help you or not, but here's a silly demo:

import random
import wx

########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "self.notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tabNum = 3

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")
        btn = wx.Button(panel, label="Add Page")
        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL, 5)

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def onButton(self, event):
        """"""
        tab = TabPanel(self.notebook)
        self.notebook.AddPage(tab, "Tab %s" % self.tabNum)
        self.tabNum += 1

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()
避讳 2024-10-20 15:45:16

肯定存在一些线程问题。看看我在 Ubuntu 上运行你的代码时遇到的这些错误:

jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
The program 'python' received an X Window System error.
This probably reflects a bug in the program.
The error was 'RenderBadPicture (invalid Picture parameter)'.
  (Details: serial 1066 error_code 161 request_code 149 minor_code 6)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)
jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.

There's definitely some threading issues. Check out these errors I get on Ubuntu when running your code:

jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
The program 'python' received an X Window System error.
This probably reflects a bug in the program.
The error was 'RenderBadPicture (invalid Picture parameter)'.
  (Details: serial 1066 error_code 161 request_code 149 minor_code 6)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)
jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文