新的 wxNotebook-Pages 在 wxPython 中保持为空
我制作了一些示例代码来尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是线程问题,而不是笔记本问题。按如下方式修改代码将创建您期望的表单。我最好的猜测是这是一个范围问题,但我不确定。
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.
我不知道这是否对您有帮助,但这是一个愚蠢的演示:
I don't know if this will help you or not, but here's a silly demo:
肯定存在一些线程问题。看看我在 Ubuntu 上运行你的代码时遇到的这些错误:
There's definitely some threading issues. Check out these errors I get on Ubuntu when running your code: