Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:44:55+00:00 2026-06-04T03:44:55+00:00

I am using python 2.7 and the latest version of wxpython. I need to

  • 0

I am using python 2.7 and the latest version of wxpython.

I need to place 2 grids side by side and to be able to add rows dynamically. Ideally, the sizes of grids should grow dynamically, but “big enough” size should be ok for the time being.

How should I change (and simplify) my code to make this happen?

class MatricesFrame(wx.BoxSizer):        
    def __init__(self, parentPanel):
        super(MatricesFrame, self).__init__(wx.HORIZONTAL)

        self.outputsMatrix = self.addEmptyGrid(parentPanel)
        self.inputsMatrix = self.addEmptyGrid(parentPanel)

        addRowsButton = wx.Button(parentPanel, -1, " Add Rows")
        addRowsButton.Bind(wx.EVT_BUTTON, self.addRows)
        self.Add(addRowsButton)

    def initResize(self, ev = None):
        self.inputsMatrix.SetSize((500, 500)) 
        self.outputsMatrix.SetSize((500, 500)) 

    def addEmptyGrid(self, parentPanel):   
        panel_ = wx.Panel(parentPanel)
        sizer_ = wx.BoxSizer(wx.VERTICAL)
        panel_.SetSizer(sizer_)
        panel_.SetSize((500, 500)) 
        matrix_ = wx.grid.Grid(panel_)
        matrix_.SetRowLabelSize(0) 
        matrix_.SetColLabelSize(0) 
        matrix_.CreateGrid(1,1)
        sizer_.Add(matrix_)
        self.Add(panel_)
        return matrix_

    def addRows(self, ev=None):   
        self.inputsMatrix.AppendRows(1)
        self.outputsMatrix.AppendRows(1)

class TestFrame(wx.Frame):
    def __init__(self, parent):
        super(TestFrame, self).__init__(parent, title='test', size=(1280, 950))               
        panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        self.matricesFrame = MatricesFrame(panel)      
        box.Add(self.matricesFrame)
        panel.SetSizer(box)
        self.matricesFrame.initResize()
        self.Centre()
        self.Show()
        self.matricesFrame.initResize()
        wx.EVT_IDLE(wx.GetApp(),  wx.WakeUpIdle())           

def main():
    app = wx.App(False)
    t= TestFrame(None)
    app.MainLoop()

if __name__ == '__main__':
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:44:57+00:00Added an answer on June 4, 2026 at 3:44 am

    For starters, if you convert your custom Sizer into a custom Panel I think it makes your code a lot easier to control. Take a look at what I came up with

    class MatricesPanel(wx.Panel):        
        def __init__(self, parent):
            super(MatricesPanel, self).__init__(parent)
    
            sizer = wx.BoxSizer(wx.HORIZONTAL)
    
            self.outputsMatrix = self.addEmptyGrid(sizer)
            self.inputsMatrix = self.addEmptyGrid(sizer)
    
            addRowsButton = wx.Button(self, -1, " Add Rows")
            addRowsButton.Bind(wx.EVT_BUTTON, self.addRows)
            sizer.Add(addRowsButton)
    
            self.SetSizer(sizer)
            self.SetAutoLayout(True)
            self.Layout()
    
        def initResize(self, ev = None):
            self.inputsMatrix.SetSize((500, 500)) 
            self.outputsMatrix.SetSize((500, 500))
            self.Layout()
    
        def addEmptyGrid(self, sizer):   
            matrix_ = wx.grid.Grid(self)
            matrix_.SetRowLabelSize(0) 
            matrix_.SetColLabelSize(0) 
            matrix_.CreateGrid(1,1)
            sizer.Add(matrix_)
            return matrix_
    
        def addRows(self, ev=None):   
            self.inputsMatrix.AppendRows(1)
            self.outputsMatrix.AppendRows(1)
            self.Layout() #refresh the frame
    

    Working with a Panel instead of a Sizer you greatly simplify your “addEmptyGrid” method as well as now you can try setting the size of your matricies using the size of the panel, not the size of the matrices themselves. Also, this allows you the flexibility to change from wx.Panel to wx.lib.scrolledpanel.ScrolledPanel if you wanted to add scroll bars (for if you add a lot of rows).

    You can then init your new panel as below:

    class TestFrame(wx.Frame):
        def __init__(self, parent):
            super(TestFrame, self).__init__(parent, title='test', size=(1280, 950))
            self.matricesPanel = MatricesPanel(self)
    
            sizer = wx.BoxSizer()
            sizer.Add(self.matricesPanel, flag=wx.EXPAND)
            self.SetSizer(sizer)
    
            self.Centre()
            self.Show()
            self.matricesPanel.initResize()
    
            wx.EVT_IDLE(wx.GetApp(),  wx.WakeUpIdle())
    

    Finally, so far as I can tell, columns in a Grid have a fixed width, so your matrix.SetSize((500, 500)) calls arent’ doing much. If you can find a way to set the width of the column then I suggest you rewrite initResize() to set the width relative to the width of the panel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the latest version of Google App Engine, Python 2.7 and webapp2, but
In my latest Python project, utilizing Twisted, I've tried to be good at using
Using Python I need to insert a newline character into a string every 64
Using Python I need to delete all characters in a multiline string up to
I'm running Windows 7, Python 2.6.4 and the latest version of Eclipse. I downloaded
I downloaded the latest version of Boost and I'm trying to get the Boost.python
I am using a text editor (Sublime Text) for programming in Python. The latest
Using Python 2.6, Twisted 10.1, and GTK+ 2.22, with latest pygtk installed, I'm having
html5lib notes that it's latest release (0.11) is somewhat old. Using the Python portion,
I'm using the latest version of suds ( https://fedorahosted.org/suds/ ) for the first time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.