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

  • SEARCH
  • Home
  • 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 8335653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:44:14+00:00 2026-06-09T03:44:14+00:00

Could someone help me kill two birds with one stone? I’ve been trying to

  • 0

Could someone help me kill two birds with one stone? I’ve been trying to center a button in my wxPanel for a little while and can’t seem to figure it out. I’ve tried different attributes such as wx.CENTER and wx.ALIGN_CENTER and wx.ALIGN_CENTER_HORIZONTAL, but just can’t seem to get the button to move to the center of the panel. The button I’m trying to center is set_button in the add_unit_pane method.

Also, when I click the X button to close the program, the program crashes. Am I not handling the close event correctly?

Could someone point out where I’m wrong?

import wx

class Example(wx.Frame):

    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw) 
        self.init_ui()

    def init_ui(self):
        self.Bind(wx.EVT_CLOSE, self.on_close_window)

        panel = wx.Panel(self)
        panel_sizer = wx.BoxSizer(wx.VERTICAL)
        panel_sizer.Add(self.add_unit_pane(panel, panel_sizer), 0, wx.EXPAND, border=5)
        panel.SetSizerAndFit(panel_sizer)

        self.Show()

    def on_close_window(self, event):
        self.Destroy()

    def add_unit_pane(self, panel, panel_sizer):
        """ Creates the Unit Measurement panel """
        unit_panel = wx.StaticBox(panel, -1, 'Unit of Measurement')
        unit_panel_sizer = wx.StaticBoxSizer(unit_panel, wx.VERTICAL)

        # Create horizontal row of widgets 1
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)

        st1 = wx.StaticText(panel, label='UNIT:')
        unit_choices = ['in (INCHES)',
                        'cm (CENTIMETERS)',
                        'mm (MILLIMETERS)'
                        ]
        unit_cb = wx.ComboBox(panel, -1, '100%',
                                     choices=unit_choices,
                                     style=wx.CB_DROPDOWN|wx.CB_READONLY)
        unit_cb.SetValue('mm (MILLIMETERS)')

        hbox1.Add(st1)
        hbox1.AddSpacer(5)
        hbox1.Add(unit_cb)

        # Create horizontal row of widgets 2
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)

        st2 = wx.StaticText(panel, label='Pixels per selected unit:')
        tc1 = wx.TextCtrl(panel)

        hbox2.Add(st2)
        hbox2.AddSpacer(5)
        hbox2.Add(tc1)

        # Create horizontal row of widgets 3
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        set_button = wx.Button(panel, -1, 'Set')
        hbox3.Add(set_button, 0, wx.CENTER)

        # Add all other sizers to the main unit_panel_sizer
        unit_panel_sizer.Add(hbox1)
        unit_panel_sizer.AddSpacer(5)
        unit_panel_sizer.Add(hbox2)
        unit_panel_sizer.AddSpacer(5)
        unit_panel_sizer.Add(hbox3)

        # Fit all widgets to the sizer
        unit_panel.SetSizerAndFit(unit_panel_sizer)

        # Return the unit_panel_sizer
        return unit_panel_sizer

if __name__ == '__main__':
    ex = wx.App()
    Example(None, -1, "Centering Button", size=(250,150))
    ex.MainLoop()

Thank you,
Adam

  • 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-09T03:44:15+00:00Added an answer on June 9, 2026 at 3:44 am

    change the line where you add hbox3 to be

    unit_panel_sizer.Add(hbox3,0,wx.CENTER)
    

    also comment out

    unit_panel.SetSizerAndFit(unit_panel_sizer) 
    

    that should fix your crashing

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

Sidebar

Related Questions

Could someone help me explain this? I have two snippets of code, one works
Please could someone help diagnose this tiny problem that shouldn't be one - trying
Could someone help me afloat here? Still new to F#, actually trying to use
Could someone help me resolve the discrepancies between the following two cases. In both
Could someone help me with the following two questions? How can I filter on
I'm a little noobish to perl coding conventions, could someone help explain: why are
Could someone help me to add A-atype B-btype C-ctype ONLY as a legend to
Could someone help me to write a function that checks if a string is
Could someone help me to write a logic that converts a ID array to
could someone help me on this problem, i want to access facebook API through

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.