1

I am trying to create a new frame in a wxpanel by clicking a button.

Below is my code. It does not work. Scroll bars do not show up.

Can anyone help me? Thanks!

(update: A button is added in the new window)

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

,

(update 2) based on Joran Beasley's code,

this code creates scroll bars, but the button2 is not shown. and the text widget does not work properly when scrolled.

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

,

(update 3) I found my mistake. The widgets should be on the scroll object, not on the frame object. Layout() and Fit() are not required. so correct code is

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()
vandy
  • 13
  • 4
  • 1
    Can you explain "does not work"? – C R Dec 31 '14 at 17:50
  • your code works fine for me, meaning that it pops up a window labeled 'test Panel', with a button on it labeled 'New Frame'. When the button is clicked, a new window labeled 'frame2' appears – stochastic Dec 31 '14 at 17:57
  • It opens a new frame, but no scroll bars. I cannot upload images here. so I put my image in my google drive. please click this, https://drive.google.com/file/d/0B5FAgE3R3ldRck4xM2UtY3htZ28/view?usp=sharing – vandy Dec 31 '14 at 18:09
  • the scrollbars are there ... you just cant see them... – Joran Beasley Dec 31 '14 at 18:26

1 Answers1

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

you need to layout the new window ... since you clearly want it to fill the 500,500 area you will need to use 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()

or just force the size of the contained scrollwindow (which is what you normally do for scrolled windows)

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()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Thanks for your answer. It creates a window with scroll bars, but the size is not (500,500), and if I add a button, self.btn2 = wx.Button(self.new_window, pos=(50,100), label="button2") just below your code, it becomes broken again. – vandy Dec 31 '14 at 18:29
  • see new edit ... that should force it to fill the space – Joran Beasley Dec 31 '14 at 18:37
  • Thanks for your 2nd answer!, but if I add a button object, it behaves weird when I scroll it. – vandy Dec 31 '14 at 18:43
  • what do you mean? anyway thats probably best for another question – Joran Beasley Dec 31 '14 at 18:43
  • If I add a button and scroll it vertically, it becomes, https://drive.google.com/file/d/0B5FAgE3R3ldRX0s2eTJDekY4S1E/view?usp=sharing , the button does not stay at a fixed position :( – vandy Dec 31 '14 at 18:48
  • and if I add a button after 'self.new_window.Show()', it does not show up. – vandy Dec 31 '14 at 19:10