我正在尝试通过单击按钮在wxpanel中创建新框架。
下面是我的代码。它不起作用。滚动条不显示。
谁能帮我?谢谢!
(更新:新窗口中添加一个按钮)
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)根据乔兰·比斯利的代码,
此代码创建滚动条,但不显示 button2。并且文本小部件在滚动时无法正常工作。
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()
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
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()
您需要布局新窗口...由于您显然希望它填充 500,500 个区域,因此您需要使用尺寸仪
def newFrame(self, event):
self.new_window = wx.Frame(self, title='frame2', size=(500, 500), pos=(800,0))
sz = wx.BoxSizer()
sz.SetMinSize((500,500)) #force minimum size
self.scroll = wx.ScrolledWindow(self.new_window, -1)
sz.Add(self.scroll,1,wx.EXPAND)
self.scroll.SetScrollbars(1, 1, 1600, 1400)
self.new_window.SetSizer(sz)
self.new_window.Layout()
self.new_window.Fit()
self.new_window.Show()
或者只是强制包含的滚动窗口的大小(这是您通常对滚动窗口所做的)
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()