1

我正在尝试通过单击按钮在 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)基于 Joran Beasley 的代码,

此代码创建滚动条,但未显示 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()
4

1 回答 1

1
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 区域,您将需要使用 sizers

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()
于 2014-12-31T18:12:19.417 回答