通过事件创建wxpython滚动窗口(框架)

时间:2014-12-31 17:47:42

标签: python wxpython scrolledwindow

我正在尝试通过单击按钮在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()

1 个答案:

答案 0 :(得分: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区域,你需要使用sizer

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()