我试图通过单击按钮在wxpanel中创建一个新框架。通过事件创建一个wxpython滚动窗口(框架)
下面是我的代码。 它不起作用。 滚动条不显示。
任何人都可以帮助我吗?谢谢!
(更新:一个按钮,在新窗口中添加)
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
self.new_window.Show()
self.scroll = wx.ScrolledWindow(self.new_window, -1)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2")
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
,
(更新2) 基于Joran比斯利的代码,
此代码创建滚动条,但按钮2未显示。 和滚动时文本小部件无法正常工作。
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', pos=(800,0))
self.scroll = wx.ScrolledWindow(self.new_window, -1,size=(500,500))
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2")
wx.StaticText(self.new_window, -1, 'test text', pos=(50, 200))
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
,
(更新3) 我发现我错了。这些小部件应该位于滚动对象上,而不是框架对象上。 Layout()和Fit()不是必需的。 所以正确的代码是
import wx
class ftest(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"test Panel", size=(800, 500), pos=(0,0))
self.MainPanel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self.MainPanel, pos=(50,100), label="New Frame")
self.btn1.Bind(wx.EVT_BUTTON, self.newFrame)
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', pos=(800,0),size=(500,500))
self.scroll = wx.ScrolledWindow(self.new_window, -1)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
#self.new_window.Layout()
#self.new_window.Fit()
self.new_window.Show()
self.btn2 = wx.Button(self.scroll, pos=(50,100), label="button2")
wx.StaticText(self.scroll, -1, 'test text', pos=(50, 200))
if __name__ == "__main__":
app = wx.App(False)
frame = ftest()
frame.Show()
app.MainLoop()
你能解释“不起作用“? – rapvelopment
您的代码对我来说工作正常,这意味着它会弹出一个标记为“测试面板”的窗口,并在其上标记为“新建”的按钮。点击该按钮后,将出现一个标有“frame2”的新窗口 – stochastic
它打开一个新框架,但没有滚动条。我无法在此上传图片。所以我把我的图片放在我的谷歌驱动器中。请点击此,https://drive.google.com/file/d/0B5FAgE3R3ldRck4xM2UtY3htZ28/view?usp=sharing – vandy