I want to add a white color to the thickness box which is currently appearing as grey as in the image. Any help to solve
Below is the code
sbSizer4 = wx.StaticBoxSizer( wx.StaticBox( self.m_panel_geometry, wx.ID_ANY, u"Thickness" ), wx.VERTICAL )
sbSizer4.Add( ( 0, 0), 1, wx.EXPAND, 5 )
m_thicknessChoices = [ u"0.062 in", u"0.031 in" ]
self.m_thickness = wx.Choice( sbSizer4.GetStaticBox(), wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_thicknessChoices, 0 )
self.m_thickness.SetSelection( 0 )
#self.m_thickness.SetBackgroundColour(wx.Colour(240, 0, 240))
sbSizer4.Add( self.m_thickness, 0, wx.ALIGN_CENTER|wx.ALL|wx.EXPAND, 15 )
sbSizer4.Add( ( 0, 0), 1, wx.EXPAND, 5 )
bSizer11.Add( sbSizer4, 1, wx.ALL|wx.EXPAND, 10 )
bSizer9.Add( bSizer11, 1, wx.EXPAND, 5 )
bSizer12 = wx.BoxSizer( wx.HORIZONTAL )
It took a while to realise that:
Your code was not indented properly (not helped by another edit)
You're using a StaticBox within a StaticBoxSizer
You must be using an older version of wxPython
Try removing one of the static boxes, there seems little to be gained from having 2 of them, whilst only labeling one.
You specify white in the question but in the code you attempted a vile puce/pink colour, I've run with that ;).
import wx
class TestFrame(wx.Frame):
def __init__(self, parent, title="Static Box"):
wx.Frame.__init__(self, parent, -1, title)
panel = wx.Panel(self)
panel.SetBackgroundColour("palegreen")
bSizer9 = wx.BoxSizer( wx.VERTICAL )
bSizer11 = wx.BoxSizer( wx.VERTICAL )
sbSizer4 = wx.StaticBoxSizer( wx.VERTICAL, panel, "Thickness")
sbSizer4.Add( ( 0, 0), 1, wx.EXPAND, 5 )
m_thicknessChoices = [ u"0.062 in", u"0.031 in" ]
self.m_thickness = wx.Choice( sbSizer4.GetStaticBox(), wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_thicknessChoices, 0 )
self.m_thickness.SetSelection( 0 )
self.m_thickness.SetBackgroundColour(wx.Colour(240, 0, 240))
sbSizer4.Add( self.m_thickness, 0, wx.ALL|wx.EXPAND, 15 )
sbSizer4.Add( ( 0, 0), 1, wx.EXPAND, 5 )
bSizer11.Add( sbSizer4, 0, wx.ALL|wx.EXPAND, 10 )
bSizer9.Add( bSizer11, 1, wx.EXPAND, 5 )
panel.SetSizer(bSizer9)
self.Show()
if __name__ == '__main__':
app = wx.App()
TestFrame(None)
app.MainLoop()
Replacing wx.Choice() with BitmapComboBox() and style=wx.CB_READONLY fixed the color issue for me. There's some odd fading in and out when changing the selection though (at least on Win 10). wxPython is really good at making simple things hard.
All im trying to do is have 2 classes
1- creates a grid
2- takes the grid and puts it into a wx.notebook
so basically one class makes the grid the other class takes the grid as parameter and add it to the wx.notebook
but I keep getting an error that says
self.m_grid1 = wx.grid.Grid(self) TypeError: Grid(): arguments did not match any overloaded call:
overload 1: too many arguments
overload 2: argument 1 has unexpected type 'reportGrid'
and here is the code for the Grid class is called
reportGrid
class reportGrid ():
def __init__( self, list):
self.m_grid1 = wx.grid.Grid(self)
self.m_grid1.Create(parent = None, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.WANTS_CHARS, name="Grid")
# Grid
self.m_grid1.CreateGrid( 7, 18 )
self.m_grid1.EnableEditing( True )
self.m_grid1.EnableGridLines( True )
self.m_grid1.SetGridLineColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_WINDOWTEXT ) )
self.m_grid1.EnableDragGridSize( True )
self.m_grid1.SetMargins( 0, 0 )
# Columns
self.m_grid1.EnableDragColMove( False )
self.m_grid1.EnableDragColSize( True )
self.m_grid1.SetColLabelSize( 30 )
self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
# Rows
self.m_grid1.EnableDragRowSize( True )
self.m_grid1.SetRowLabelSize( 80 )
self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
# Label Appearance
self.m_grid1.SetColLabelValue(0, "Yield")
self.m_grid1.SetColLabelValue(1, "64CU")
self.m_grid1.SetColLabelValue(2, "Yield")
self.m_grid1.SetColLabelValue(3, "60CU")
self.m_grid1.SetColLabelValue(4, "Chain")
self.m_grid1.SetColLabelValue(5, "Logic")
self.m_grid1.SetColLabelValue(6, "Delay")
self.m_grid1.SetColLabelValue(7, "BIST")
self.m_grid1.SetColLabelValue(8, "CREST")
self.m_grid1.SetColLabelValue(9, "HSIO")
self.m_grid1.SetColLabelValue(10, "DC-Spec")
self.m_grid1.SetColLabelValue(11, "HBM")
self.m_grid1.SetColLabelValue(12, "OS")
self.m_grid1.SetColLabelValue(13, "PS")
self.m_grid1.SetColLabelValue(14, "Alarm")
self.m_grid1.SetColLabelValue(15, "JTAG")
self.m_grid1.SetColLabelValue(16, "Thermal IDD")
self.m_grid1.SetColLabelValue(17, "Insuff Config")
self.m_grid1.SetRowLabelValue(0, "Today")
self.m_grid1.SetRowLabelValue(1, "WTD")
self.m_grid1.SetRowLabelValue(2, "WW45")
self.m_grid1.SetRowLabelValue(3, "WW44")
self.m_grid1.SetRowLabelValue(4, "WW43")
self.m_grid1.SetRowLabelValue(5, "Monthly")
self.m_grid1.SetRowLabelValue(6, "QTD")
# Cell Defaults
for i in range(len(list)):
for j in range(len(list[i])):
self.m_grid1.SetCellValue(i,j, list[i][j])
self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
and here the class that takes it as a parameter and suppose to create notebook
class reportFrame ( wx.Frame ):
def __init__( self, parent , grid1):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Report", pos = wx.DefaultPosition, size = wx.Size( 7990,210 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer6 = wx.BoxSizer( wx.VERTICAL )
self.m_notebook1 = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_notebook1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_INFOBK ) )
self.m_panel2 = wx.Panel( self.m_notebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer14 = wx.BoxSizer( wx.HORIZONTAL )
bSizer14.Add( grid1, 0, wx.ALL, 5 )
self.m_panel2.SetSizer( bSizer14 )
self.m_panel2.Layout()
bSizer14.Fit( self.m_panel2 )
self.m_notebook1.AddPage( self.m_panel2, u"a page", False )
self.m_panel3 = wx.Panel( self.m_notebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer17 = wx.BoxSizer( wx.VERTICAL )
bSizer17.Add( grid1, 0, wx.ALL, 5 )
self.m_panel3.SetSizer( bSizer17 )
self.m_panel3.Layout()
bSizer17.Fit( self.m_panel3 )
self.m_notebook1.AddPage( self.m_panel3, u"a page", True )
bSizer6.Add( self.m_notebook1, 1, wx.EXPAND |wx.ALL, 3 )
self.SetSizer( bSizer6 )
self.Layout()
self.Centre( wx.BOTH )
self.Show(show=True)
wx.grid.Grid(self) here self must be a wx.Window (or subclass) type. In your code it's reportGrid type.
But reportGrid is not a wx.Window nor a subclass of wx.Window.
If you have a page "pagegrid" (for example, of type wx.Panel or subclass) of the wx.Notebook then you can set
class reportGrid (wx.Panel):
def __init__( self, list):
self.m_grid1 = wx.grid.Grid(self)
and inside your notebook definition
pagegrid = reportGrid(nb)
nb.AddPage(pagegrid , "Grid Page")
I'm using wxpython to code this simple form. A notebook with a scroll bar and few text controls is what i have used.I can see the widgets which are view-able on screen but the ones which needs to be scrolled down are not visible. In my code below i could see upto "Enter the Logs" and appropriate text control for that fields but the "review fields are missing along with submit and cancel buttons.
import wx
import wx.lib.filebrowsebutton as filebrowse
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 600,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
sizer = wx.BoxSizer( wx.VERTICAL )
self.notebook = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.login = wx.Panel( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.notebook.AddPage( self.login, u"Login", False )
self.scroll = wx.ScrolledWindow( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
vbox = wx.BoxSizer(wx.VERTICAL)
# Sizer for widgets inside tabs
inside_sizer_h1 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h2 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h3 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h4 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h5 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h6 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h7 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h8 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h9 = wx.BoxSizer(wx.HORIZONTAL)
#Test Approve Label
self.test_app_label = wx.StaticText(self.scroll, -1 , label="Test Approved By :")
inside_sizer_h1.Add(self.test_app_label, 1, wx.ALL,5)
#Test Approve Combo
self.tes_app_combo = wx.ComboBox(self.scroll, -1, value='None', choices= ['None', 'approver1', 'approver2', 'approver3', 'approver4'] )
inside_sizer_h1.Add(self.tes_app_combo, 1, wx.ALL, 5 )
#Workspace Label
self.wrksp_label = wx.StaticText(self.scroll, -1 , label="Workspace :")
inside_sizer_h2.Add(self.wrksp_label, 1, wx.ALL,5)
#Workspace file selector
self.select_wrksp_dir = filebrowse.DirBrowseButton(self.scroll, -1,labelText = "", toolTip = 'Select tip of your workspace')
inside_sizer_h2.Add(self.select_wrksp_dir, 1, wx.ALL|wx.EXPAND, 5 )
# Issuelist label
self.ar_list_label = wx.StaticText(self.scroll, -1 , label="Issue List :")
inside_sizer_h3.Add(self.ar_list_label, 1, wx.ALL,5)
# Issue Text box
self.ar_list_text = wx.TextCtrl(self.scroll, -1, value=u"Enter The issue, one per line", style=wx.TE_MULTILINE)
inside_sizer_h3.Add(self.ar_list_text, 1, wx.ALL, 5 )
# Summary of change Title
self.change_summary_label = wx.StaticText(self.scroll, -1 , label=u"Summary of change :")
inside_sizer_h4.Add(self.change_summary_label, 1, wx.ALL, 5)
# Summary of change Text Box
self.change_summary_text = wx.TextCtrl(self.scroll, -1, value=u"What componet has changed?",style=wx.TE_MULTILINE)
inside_sizer_h4.Add(self.change_summary_text, 1, wx.ALL, 5 )
# Changed File List Title
self.change_file_list_label = wx.StaticText(self.scroll, -1 , label=u"Changed File List :")
inside_sizer_h5.Add(self.change_file_list_label,1, wx.ALL, 5)
# Changed File List Box
self.change_summary_text = wx.TextCtrl(self.scroll, -1, u' enter list of changed files',style=wx.TE_MULTILINE)
inside_sizer_h5.Add(self.change_summary_text,1, wx.ALL, 5)
# GUI Testing done label
self.testing_done_label = wx.StaticText(self.scroll, -1 , label=u"What tests have you done? :")
inside_sizer_h6.Add(self.testing_done_label,1, wx.ALL, 5)
#FlexGUi Checkbox
self.gui_check_list = wx.CheckListBox(self.scroll, -1, choices=['GUI Builds Successfully', 'GUI Automation Tests', 'CLI Automation Tests'])
inside_sizer_h6.Add(self.gui_check_list,1, wx.ALL, 5)
# GUI Automation test logs label
self.gui_auto_log_label = wx.StaticText(self.scroll, -1 , label=u"Enter the logs :")
inside_sizer_h7.Add(self.gui_auto_log_label,1, wx.ALL, 5)
#GUI Automation test box
self.gui_auto_log = wx.TextCtrl(self.scroll, -1, u'Copy and paste the logs.',style=wx.TE_MULTILINE)
inside_sizer_h7.Add(self.gui_auto_log,1, wx.ALL, 5)
# Review URL Text
self.review_url_label = wx.StaticText(self.scroll, -1 , label=u"Code review URL :")
inside_sizer_h8.Add(self.review_url_label,1, wx.ALL, 5)
#Code Review Textbox
self.review_url_tbox = wx.TextCtrl(self.scroll, -1, value=u"Enter the code review URL",style=wx.TE_MULTILINE)
inside_sizer_h8.Add(self.review_url_tbox,1, wx.ALL, 5)
#Submit button
self.sub_button = wx.Button(self.scroll, label = 'Submit')
inside_sizer_h9.Add(self.sub_button, wx.ALL, 5)
#Cancel button
self.canc_button = wx.Button(self.scroll, label = 'Cancel')
inside_sizer_h9.Add(self.canc_button,1, wx.ALL, 5)
vbox.Add(inside_sizer_h1, 0 , wx.TOP|wx.EXPAND, 40 )
vbox.Add(inside_sizer_h2, 0 , wx.ALL|wx.EXPAND, 5 )
vbox.Add(inside_sizer_h3, 0 , wx.ALL|wx.EXPAND, 5 )
vbox.Add(inside_sizer_h4, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h5, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h6, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h7, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h8, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h9, 0 , wx.ALL|wx.EXPAND, 10)
self.Maximize()
self.scroll.Size = self.GetSize()
print self.GetSize()
self.scroll.SetScrollbars(20,25,45,50)
self.SetSizer( vbox )
self.SetSizerAndFit(vbox)
self.Layout()
self.notebook.AddPage( self.scroll, u"Delivery", True )
sizer.Add( self.notebook, 1, wx.EXPAND |wx.ALIGN_RIGHT|wx.ALL, 0 )
self.SetSizer( sizer )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
if name == "main":
app = wx.App()
Frame(None)
app.MainLoop()
Your code (amended) working on Linux:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import wx.lib.filebrowsebutton as filebrowse
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 600,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
sizer = wx.BoxSizer( wx.VERTICAL )
self.notebook = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
self.login = wx.Panel( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.notebook.AddPage( self.login, u"Login", False )
self.scroll = wx.ScrolledWindow( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
vbox = wx.BoxSizer(wx.VERTICAL)
# Sizer for widgets inside tabs
inside_sizer_h1 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h2 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h3 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h4 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h5 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h6 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h7 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h8 = wx.BoxSizer(wx.HORIZONTAL)
inside_sizer_h9 = wx.BoxSizer(wx.HORIZONTAL)
#Test Approve Label
self.test_app_label = wx.StaticText(self.scroll, -1 , label="Test Approved By :")
inside_sizer_h1.Add(self.test_app_label, 1, wx.ALL,5)
#Test Approve Combo
self.tes_app_combo = wx.ComboBox(self.scroll, -1, value='None', choices= ['None', 'approver1', 'approver2', 'approver3', 'approver4'] )
inside_sizer_h1.Add(self.tes_app_combo, 1, wx.ALL, 5 )
#Workspace Label
self.wrksp_label = wx.StaticText(self.scroll, -1 , label="Workspace :")
inside_sizer_h2.Add(self.wrksp_label, 1, wx.ALL,5)
#Workspace file selector
self.select_wrksp_dir = filebrowse.DirBrowseButton(self.scroll, -1,labelText = "", toolTip = 'Select tip of your workspace')
inside_sizer_h2.Add(self.select_wrksp_dir, 1, wx.ALL|wx.EXPAND, 5 )
# Issuelist label
self.ar_list_label = wx.StaticText(self.scroll, -1 , label="Issue List :")
inside_sizer_h3.Add(self.ar_list_label, 1, wx.ALL,5)
# Issue Text box
self.ar_list_text = wx.TextCtrl(self.scroll, -1, value=u"Enter The issue, one per line", style=wx.TE_MULTILINE)
inside_sizer_h3.Add(self.ar_list_text, 1, wx.ALL, 5 )
# Summary of change Title
self.change_summary_label = wx.StaticText(self.scroll, -1 , label=u"Summary of change :")
inside_sizer_h4.Add(self.change_summary_label, 1, wx.ALL, 5)
# Summary of change Text Box
self.change_summary_text = wx.TextCtrl(self.scroll, -1, value=u"What componet has changed?",style=wx.TE_MULTILINE)
inside_sizer_h4.Add(self.change_summary_text, 1, wx.ALL, 5 )
# Changed File List Title
self.change_file_list_label = wx.StaticText(self.scroll, -1 , label=u"Changed File List :")
inside_sizer_h5.Add(self.change_file_list_label,1, wx.ALL, 5)
# Changed File List Box
self.change_summary_text = wx.TextCtrl(self.scroll, -1, u' enter list of changed files',style=wx.TE_MULTILINE)
inside_sizer_h5.Add(self.change_summary_text,1, wx.ALL, 5)
# GUI Testing done label
self.testing_done_label = wx.StaticText(self.scroll, -1 , label=u"What tests have you done? :")
inside_sizer_h6.Add(self.testing_done_label,1, wx.ALL, 5)
#FlexGUi Checkbox
self.gui_check_list = wx.CheckListBox(self.scroll, -1, choices=['GUI Builds Successfully', 'GUI Automation Tests', 'CLI Automation Tests'])
inside_sizer_h6.Add(self.gui_check_list,1, wx.ALL, 5)
# GUI Automation test logs label
self.gui_auto_log_label = wx.StaticText(self.scroll, -1 , label=u"Enter the logs :")
inside_sizer_h7.Add(self.gui_auto_log_label,1, wx.ALL, 5)
#GUI Automation test box
self.gui_auto_log = wx.TextCtrl(self.scroll, -1, u'Copy and paste the logs.',style=wx.TE_MULTILINE)
inside_sizer_h7.Add(self.gui_auto_log,1, wx.ALL, 5)
# Review URL Text
self.review_url_label = wx.StaticText(self.scroll, -1 , label=u"Code review URL :")
inside_sizer_h8.Add(self.review_url_label,1, wx.ALL, 5)
#Code Review Textbox
self.review_url_tbox = wx.TextCtrl(self.scroll, -1, value=u"Enter the code review URL",style=wx.TE_MULTILINE)
inside_sizer_h8.Add(self.review_url_tbox,1, wx.ALL, 5)
#Submit button
self.sub_button = wx.Button(self.scroll, label = 'Submit')
inside_sizer_h9.Add(self.sub_button,1, wx.ALL, 5)
#Cancel button
self.canc_button = wx.Button(self.scroll, label = 'Cancel')
inside_sizer_h9.Add(self.canc_button,1, wx.ALL, 5)
vbox.Add(inside_sizer_h1, 0 , wx.TOP|wx.EXPAND, 40 )
vbox.Add(inside_sizer_h2, 0 , wx.ALL|wx.EXPAND, 5 )
vbox.Add(inside_sizer_h3, 0 , wx.ALL|wx.EXPAND, 5 )
vbox.Add(inside_sizer_h4, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h5, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h6, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h7, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h8, 0 , wx.ALL|wx.EXPAND, 10)
vbox.Add(inside_sizer_h9, 0 , wx.ALL|wx.EXPAND, 10)
self.Maximize()
self.scroll.Size = self.GetSize()
print self.GetSize()
self.scroll.SetScrollbars(20,25,45,50)
self.SetSizer( vbox )
self.SetSizerAndFit(vbox)
self.Layout()
self.notebook.AddPage( self.scroll, u"Delivery", True )
sizer.Add( self.notebook, 1, wx.EXPAND |wx.ALIGN_RIGHT|wx.ALL, 0 )
self.SetSizer( sizer )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
if __name__ == "__main__":
app = wx.App()
Frame(None)
app.MainLoop()
When adding your items to the sizer, you consistently set the proportion value to 1, with the exception of self.sub_button. It is that which is causing your problem.
inside_sizer_h9.Add(self.sub_button, wx.ALL, 5)
should read:
inside_sizer_h9.Add(self.sub_button,1, wx.ALL, 5)
Note also that the posted code main is incorrect and should read:
if __name__ == "__main__":
With those amendments you code should work as you wrote it.
I am trying to build a GUI with wxPython. (BTW, this problem is from work, and I am typing from home because I can't access this site from work. Maybe that's a firewall problem? Anyway, that's why I'm doing this from memory and can't give any screenshots.) My first collection of controls is a vertical stack. A static text is on top. Below it is a BoxSizer holding three text controls added horizontally. Below that is another static text. On the bottom is one final text control. The problem is the three text controls in the BoxSizer. If I add them without the proportion argument, everything generally looks good. But I want them to be different sizes. So, when I add the proportion arguments (4, 3, and 16 if that matters) the three text controls become far too large and greatly increase the width of the frame. The bottom text control expands to equal the length of the three top text controls too. So, I can get the overall size of everything right if I give up the proportions of the three text controls. Or I can get their proportions right if I give up the overall horizontal size that I want. But I really need both. Is there a way? Thanks.
A little example of code would help a lot.
I could not understand how setting the proportion of the widgets in the sizer increases the width of the frame!
Anyways, you can set the size of the textctrl widgets when creating them, which might take care of both the proportion and overall size issues. You will only need to calculate the appropriate sizes beforehand.
If it helps in any way, here's an example of a frame with the specifications you provided:
import wx
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 400,400 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
sizer = wx.BoxSizer( wx.VERTICAL )
self.panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
sizer_0 = wx.BoxSizer( wx.VERTICAL )
self.static_text_top = wx.StaticText( self.panel, wx.ID_ANY, u"Something Something", wx.DefaultPosition, wx.DefaultSize, 0 )
self.static_text_top.Wrap( -1 )
sizer_0.Add( self.static_text_top, 0, wx.ALL, 5 )
sizer_01 = wx.BoxSizer( wx.HORIZONTAL )
self.textCtrl1 = wx.TextCtrl( self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
sizer_01.Add( self.textCtrl1, 4, wx.ALL, 5 )
self.textCtrl2 = wx.TextCtrl( self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
sizer_01.Add( self.textCtrl2, 3, wx.ALL, 5 )
self.textCtrl3 = wx.TextCtrl( self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
sizer_01.Add( self.textCtrl3, 16, wx.ALL, 5 )
sizer_0.Add( sizer_01, 0, wx.EXPAND, 0 )
self.static_text_middle = wx.StaticText( self.panel, wx.ID_ANY, u"Something else", wx.DefaultPosition, wx.DefaultSize, 0 )
self.static_text_middle.Wrap( -1 )
sizer_0.Add( self.static_text_middle, 0, wx.ALL, 5 )
self.textCtrl4 = wx.TextCtrl( self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
sizer_0.Add( self.textCtrl4, 1, wx.ALL|wx.EXPAND, 5 )
self.panel.SetSizer( sizer_0 )
self.panel.Layout()
sizer_0.Fit( self.panel )
sizer.Add( self.panel, 1, wx.EXPAND |wx.ALL, 0 )
self.SetSizer( sizer )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
if __name__ == "__main__":
app = wx.App()
Frame()
app.MainLoop()
As pointed out in some other topics, wx.MessageDialog doesn't respond to many API functions, such as an external call from Destroy(), etc. So there is a need for building a wx.GenericMessageBox derived from wx.Dialog.
Here it is :
import wx
class GenericMessageBox(wx.Dialog):
def __init__(self, parent, text, title = ''):
wx.Dialog.__init__(self, parent, -1, title = title, size = (360,120), style = wx.DEFAULT_DIALOG_STYLE)
panel = wx.Panel(self, wx.ID_ANY, size = (360, 50), pos = (0,0))
panel.SetBackgroundColour('#FFFFFF')
label = wx.StaticText(panel, -1, text, pos = (50,20))
panel2 = wx.Panel(self, wx.ID_ANY, size = (360, 40), pos = (0, 50))
btn = wx.Button(panel2, wx.ID_OK, pos = (250,7))
self.ShowModal()
app = wx.App()
frame = wx.Frame(None, 0, 'Test')
frame.Show()
GenericMessageBox(frame, 'This is a message box that is derived from wx.Dialog. You can Destroy() it from anywhere in the code.', 'Test')
app.MainLoop()
Unlike wx.lib.agw.genericmessagedialog, this one's goal is to have the closest look possible to native OS look (here Windows look). [genericmessagedialog has pictures in the buttons, which is not like Windows' native look]
How would it be possible to improve this dialog in order that it its size is automatically increased if StaticText needs two lines?
Moreover the OK button (x,y) positionning is okay and centered on the grey panel on my machine, but would it be the same on other platforms?
(I think such a snippet could be useful for community.)
To manage the Layout, it is better to use Sizer instead of position.
You can have a try with following code:
use self.setMessageLine(Num) to set the output to see the difference.
import wx
class MyDialog ( wx.Dialog ):
def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,200 ), style = wx.DEFAULT_DIALOG_STYLE )
#set the minimum Size of the frame to Fit()
self.SetSizeHintsSz( wx.Size( 300,-1 ), wx.DefaultSize )
fgSizer = wx.FlexGridSizer( 2, 1, 0, 0 )
fgSizer.AddGrowableCol( 0 )
fgSizer.AddGrowableRow( 0 )
fgSizer.SetFlexibleDirection( wx.BOTH )
fgSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_ALL )
self.m_panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SIMPLE_BORDER|wx.TAB_TRAVERSAL )
gSizer = wx.GridSizer( 1, 1, 0, 0 )
self.m_staticText = wx.StaticText( self.m_panel, wx.ID_ANY, u"test", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText.Wrap( 10 )
gSizer.Add( self.m_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
self.m_panel.SetSizer( gSizer )
self.m_panel.Layout()
gSizer.Fit( self.m_panel )
fgSizer.Add( self.m_panel, 1, wx.EXPAND |wx.ALL, 5 )
m_sdbSizer = wx.StdDialogButtonSizer()
self.m_sdbSizerOK = wx.Button( self, wx.ID_OK )
m_sdbSizer.AddButton( self.m_sdbSizerOK )
self.m_sdbSizerCancel = wx.Button( self, wx.ID_CANCEL )
m_sdbSizer.AddButton( self.m_sdbSizerCancel )
m_sdbSizer.Realize()
fgSizer.Add( m_sdbSizer, 1, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( fgSizer )
self.Layout()
self.Centre( wx.BOTH )
self.setMessageLine(10)
def setMessageLine(self, messageLine):
msg = ""
for i in range(0, messageLine):
msg += "Line %d \n" % i
self.m_staticText.SetLabel(msg)
self.Fit()
if __name__ == '__main__':
app = wx.App()
dlg = MyDialog(None)
dlg.ShowModal()
app.MainLoop()
pass