473,546 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxpython: centering buttons with sizer

75 New Member
Maybe the topic should be how to put background colors to sizers, but anyway...
could anyone please help me, I need to have "background color for buttons", like in example blue & red, and then using sizers to center buttons... this task would be so easier if only I could set background color for boxsizer =p

so please try to center these buttons with sizers without removing red&blue background
Expand|Select|Wrap|Line Numbers
  1. import wx
  2.  
  3. class MyFrame(wx.Frame):
  4.    def __init__(self):
  5.       wx.Frame.__init__(self, None, id=-1, size=(300,150))
  6.  
  7.       frame_panel = wx.Panel(self, -1)
  8.  
  9.       button1_panel = wx.Panel(frame_panel, -1) #panel for button1
  10.       button1_panel.SetBackgroundColour('BLUE')
  11.  
  12.       button2_panel = wx.Panel(frame_panel, -1) #panel for button2
  13.       button2_panel.SetBackgroundColour('RED')
  14.  
  15.       button1 = wx.Button(button1_panel, id=-1, label="button1", size=(80,20))
  16.       button2 = wx.Button(button2_panel, id=-1, label="button2", size=(80,20))
  17.  
  18.       button1_sizer = wx.BoxSizer(wx.HORIZONTAL)     #add button1 to sizer
  19.       button1_sizer.Add(button1, 0, wx.ALIGN_CENTER)
  20.       button2_sizer = wx.BoxSizer(wx.HORIZONTAL)     #add button2 to sizer
  21.       button2_sizer.Add(button1, 0, wx.ALIGN_CENTER)
  22.  
  23.       button1_panel.SetSizer(button1_sizer)          #set sizers for panel
  24.       button2_panel.SetSizer(button2_sizer)
  25.  
  26.       hbox = wx.BoxSizer(wx.HORIZONTAL)              #add panels to "main sizer"
  27.       hbox.Add(button1_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
  28.       hbox.Add(button2_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
  29.  
  30.       frame_panel.SetAutoLayout(True)
  31.       frame_panel.SetSizer(hbox)
  32.       frame_panel.Layout()
  33.  
  34. app = wx.PySimpleApp()
  35. MyFrame().Show()
  36. app.MainLoop()
  37.  
  38.  
If you want to start from "beginning" , here's the code without background colors, just insert here those colors like in first example (and use sizers)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7.    def __init__(self):
  8.       wx.Frame.__init__(self, None, id=-1, size=(300,150))
  9.  
  10.       frame_panel = wx.Panel(self, -1)
  11.  
  12.       button1 = wx.Button(frame_panel, id=-1, label="button1", size=(80,20))
  13.       button2 = wx.Button(frame_panel, id=-1, label="button2", size=(80,20))
  14.  
  15.       hbox = wx.BoxSizer(wx.HORIZONTAL)
  16.       hbox.Add(button1, 1, wx.ALIGN_CENTER|wx.ALL, 25)
  17.       hbox.Add(button2, 1, wx.ALIGN_CENTER|wx.ALL, 25)
  18.  
  19.       frame_panel.SetAutoLayout(True)
  20.       frame_panel.SetSizer(hbox)
  21.       frame_panel.Layout()
  22.  
  23. app = wx.PySimpleApp()
  24. MyFrame().Show()
  25. app.MainLoop()
  26.  
Aug 27 '08 #1
1 6891
I noticed you have the sizer set on the panel named frame_panel. What if the frame had two panels, frame_panel_1 and frame_panel_2, each panel with one button?
Oct 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5285
by: Miki Tebeka | last post by:
Hello All, I have a frame that contains a panel and several buttons. I'd like to make one of the button the default button but self.SetDefaultItem(btn) or btn.SetFocus() don't work. The item in focus is a text control inside the panel. Any Ideas? (see short example below) Thanks.
15
2884
by: Grant Edwards | last post by:
Can anybody recommend a good book on wxPython? Are there any books on wxPython? I've been trying to learn wxPython and/or wax for a few weeks, and I'm just not getting it. wxWindows seems to be more low-level than the other GUI toolkits I've used (Tk, GTK, and Trestle), and there are all sorts exposed details in wxWindows/wxPython...
1
2146
by: mdk.R | last post by:
Hello all: i'am installed wxPython 2.5 and Python2.3.4..i try execute script with wxPython but it show error: Traceback (most recent call last): File "E:\py\test.py", line 7, in ? import wx File "E:\py\wx.py", line 10, in ? from wxPython.wx import * File "D:\Python23\Lib\site-package import _wx
7
1814
by: John Salerno | last post by:
I was reading in the wxPython wiki that most of the time you don't have to include the id parameter at all, and you can just use keyword arguments for other parameters. But I'm having trouble converting this code into that method (i.e., without the id parameter). I keep getting errors that involve wrong parameters, or that they are out of...
1
6063
by: Grant | last post by:
Hi, I am looking for a tip. I have a panel and a checkbox. When I check the checkbox, I would like to add buttons to the panel (dynamically). When the checkbox is unchecked, the buttons should not appear (be deleted)---all the while, the window should resize if necessary. If you have a simpler example, that is fine. I just need a hint as...
9
4407
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the "Run" button, the GUI would close, and the 12 input variables would then be available for the rest of the program. So far, what I have been able to...
0
1931
by: Soren | last post by:
Hi, I'm trying to create a small GUI program where I can do plots using Matplotlib. I've been trying to borrow code from the examples at the matplotlib website, but I can't get it to work. I want to be able to create a wx.Panel that contains an axis for plotting. Around it i want other panels containing various settings, buttons etc. to...
0
3389
by: Soren | last post by:
Hi, I've been trying to embed matplotlib in wxpython. I want to be able to put a figure (axes) in a wx.Panel and place it somewhere in my GUI. The GUI should have other panels with buttons etc. that can control the output on the figure. I've been looking at the examples from the matplotlib website, but can't seem to get it to work.. Does...
3
2300
by: Soren | last post by:
Hi, Id like to make my own special listbox.. I want to able (at the push of a button) to add another item to my special listbox... each item is a panel with a label, some buttons and maybe a text control. I've tried adding a new panel object with the stuff i want to the sizer i'm using for my listbox (which is a panel which can contain...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.