【问题标题】:creating a wxpython scrolled window (frame) by an event通过事件创建 wxpython 滚动窗口(框架)
【发布时间】:2014-12-31 17:47:42
【问题描述】:

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

【问题讨论】:

  • 你能解释一下“不起作用”吗?
  • 您的代码对我来说运行良好,这意味着它会弹出一个标有“测试面板”的窗口,上面有一个标有“新框架”的按钮。单击按钮时,会出现一个标有“frame2”的新窗口
  • 它打开一个新框架,但没有滚动条。我不能在这里上传图片。所以我把我的图片放在我的谷歌驱动器中。请点击这里,drive.google.com/file/d/0B5FAgE3R3ldRck4xM2UtY3htZ28/…
  • 滚动条在那里......你只是看不到它们......

标签: python wxpython scrolledwindow


【解决方案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()

【讨论】:

  • 感谢您的回答。它创建一个带有滚动条的窗口,但大小不是(500,500),如果我添加一个按钮,self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2") 就在下面你的代码,它又坏了。
  • 查看新的编辑...应该强制它填充空间
  • 感谢您的第二个回答!但是如果我添加一个按钮对象,当我滚动它时它的行为会很奇怪。
  • 什么意思?无论如何,这可能是另一个问题的最佳选择
  • 如果我添加一个按钮并垂直滚动它,它会变成drive.google.com/file/d/0B5FAgE3R3ldRX0s2eTJDekY4S1E/…,按钮不会停留在固定位置:(
猜你喜欢
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多