Sizers not working as expected in dialogs

I’m still a beginner with sizers, so I’m sure this is just something simple I’m neglecting.

Here is my custom dialog class:

class SizeDialog(wx.Dialog):
def init(self, parent, id, title, defsize=(0,0)):
wx.Dialog.init(self, parent, id, title, size=(300,120))

    panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)
    hbox = wx.BoxSizer(wx.HORIZONTAL)
    buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)

    self.txtWidth = wx.TextCtrl(panel, -1, str(defsize[0]))
    label = wx.StaticText(panel, -1, 'X')
    self.txtHeight = wx.TextCtrl(panel, -1, str(defsize[1]))
    hbox.Add(self.txtWidth, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(label, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(self.txtHeight, 1, wx.ALIGN_CENTER_VERTICAL)

    vbox.Add(hbox, 1, wx.LEFT|wx.RIGHT|wx.EXPAND|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)
    vbox.Add(buttons, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 10)

    panel.SetSizer(vbox)

It’s not working like I expected; everything is squished together in the corner (as if the sizers didn’t exist).

What happens if at the end you add:

panel.Layout()

Che

···

On Fri, Jul 23, 2010 at 9:14 PM, 音本四 onpon4@gmail.com wrote:

I’m still a beginner with sizers, so I’m sure this is just something simple I’m neglecting.

Here is my custom dialog class:

class SizeDialog(wx.Dialog):
def init(self, parent, id, title, defsize=(0,0)):

    wx.Dialog.__init__(self, parent, id, title, size=(300,120))

    panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)
    hbox = wx.BoxSizer(wx.HORIZONTAL)
    buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)



    self.txtWidth = wx.TextCtrl(panel, -1, str(defsize[0]))
    label = wx.StaticText(panel, -1, 'X')
    self.txtHeight = wx.TextCtrl(panel, -1, str(defsize[1]))
    hbox.Add(self.txtWidth, 1, wx.ALIGN_CENTER_VERTICAL)


    hbox.Add(label, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(self.txtHeight, 1, wx.ALIGN_CENTER_VERTICAL)

    vbox.Add(hbox, 1, wx.LEFT|wx.RIGHT|wx.EXPAND|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)

    vbox.Add(buttons, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 10)


    panel.SetSizer(vbox)

It’s not working like I expected; everything is squished together in the corner (as if the sizers didn’t exist).

Everything disappears. Or, at least, I can’t see any of the widgets.

···

On Fri, Jul 23, 2010 at 9:52 PM, C M cmpython@gmail.com wrote:

On Fri, Jul 23, 2010 at 9:14 PM, 音本四 onpon4@gmail.com wrote:

I’m still a beginner with sizers, so I’m sure this is just something simple I’m neglecting.

Here is my custom dialog class:

class SizeDialog(wx.Dialog):
def init(self, parent, id, title, defsize=(0,0)):

    wx.Dialog.__init__(self, parent, id, title, size=(300,120))

    panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)
    hbox = wx.BoxSizer(wx.HORIZONTAL)
    buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)




    self.txtWidth = wx.TextCtrl(panel, -1, str(defsize[0]))
    label = wx.StaticText(panel, -1, 'X')
    self.txtHeight = wx.TextCtrl(panel, -1, str(defsize[1]))
    hbox.Add(self.txtWidth, 1, wx.ALIGN_CENTER_VERTICAL)



    hbox.Add(label, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(self.txtHeight, 1, wx.ALIGN_CENTER_VERTICAL)

    vbox.Add(hbox, 1, wx.LEFT|wx.RIGHT|wx.EXPAND|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)


    vbox.Add(buttons, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 10)


    panel.SetSizer(vbox)

It’s not working like I expected; everything is squished together in the corner (as if the sizers didn’t exist).

What happens if at the end you add:

panel.Layout()

Che

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Everything disappears. Or, at least, I can’t see any of the widgets.

It’s harderto help without a small runnable sample (see here:
http://wiki.wxpython.org/MakingSampleApps )

but maybe what’s happening is that you should associate a sizer to self (the Dialog itself), and then add panel to that sizer and set it to expand and have proportion = 1. Something like this at the end:

dialogSizer = wx.BoxSizer(wx.VERTICAL)

self.SetSizer(dialogSizer)
dialogSizer.Add(panel,1,wx.EXPAND)
self.Layout()
panel.Layout()

(Also I think someone else mentioned that you should think about making more meaningful/apt names for objects. Calling a textCtrl “txtWidth” or a sizer hbox may add an extra layer of work for your brain that will make dealing with sizers all the harder. )

Che

···

On Fri, Jul 23, 2010 at 10:46 PM, 音本四 onpon4@gmail.com wrote:

On Fri, Jul 23, 2010 at 9:52 PM, C M cmpython@gmail.com wrote:

On Fri, Jul 23, 2010 at 9:14 PM, 音本四 onpon4@gmail.com wrote:

I’m still a beginner with sizers, so I’m sure this is just something simple I’m neglecting.

Here is my custom dialog class:

class SizeDialog(wx.Dialog):
def init(self, parent, id, title, defsize=(0,0)):

    wx.Dialog.__init__(self, parent, id, title, size=(300,120))

    panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)
    hbox = wx.BoxSizer(wx.HORIZONTAL)
    buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)





    self.txtWidth = wx.TextCtrl(panel, -1, str(defsize[0]))
    label = wx.StaticText(panel, -1, 'X')
    self.txtHeight = wx.TextCtrl(panel, -1, str(defsize[1]))
    hbox.Add(self.txtWidth, 1, wx.ALIGN_CENTER_VERTICAL)




    hbox.Add(label, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(self.txtHeight, 1, wx.ALIGN_CENTER_VERTICAL)

    vbox.Add(hbox, 1, wx.LEFT|wx.RIGHT|wx.EXPAND|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)



    vbox.Add(buttons, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 10)


    panel.SetSizer(vbox)

It’s not working like I expected; everything is squished together in the corner (as if the sizers didn’t exist).

What happens if at the end you add:

panel.Layout()

Che


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Well, this was what I used to test it:

if name == ‘main’:
app = wx.PySimpleApp()
dia = SizeDialog(None, -1, ‘test’, (16,24))
r = dia.ShowModal()
if r == wx.OK:
print ‘You pressed OK! Size: {0}x{1}’.format(dia.width.GetValue(),dia.height.GetValue())
elif r == wx.CANCEL:
print ‘You pressed Cancel!’
else:
print ‘You pressed… nothing.’
print r
dia.Destroy()
app.MainLoop()

Adding the sizer did make the TextCtrls and StaticText visible more like they were supposed to be, but the buttons still aren’t visible. Also, I don’t quite understand why explicitly defining a sizer would be necessary… It wasn’t necessary on my frame class.

No, no one mentioned anything to me about meaningful names. I was already pestered about that in Visual Basic class (heck, we even had to meaningfully name temporary loop variables!), and I have since dropped writing 20+ character long names in favor of shorter (more practical) names. Sure I could call the box something like szrTopHorizontalBox, but why use such a long and complicated name when I can just call it hbox?

···

2010/7/23 C M cmpython@gmail.com

On Fri, Jul 23, 2010 at 10:46 PM, 音本四 onpon4@gmail.com wrote:

Everything disappears. Or, at least, I can’t see any of the widgets.

It’s harderto help without a small runnable sample (see here:
http://wiki.wxpython.org/MakingSampleApps )

but maybe what’s happening is that you should associate a sizer to self (the Dialog itself), and then add panel to that sizer and set it to expand and have proportion = 1. Something like this at the end:

dialogSizer = wx.BoxSizer(wx.VERTICAL)

self.SetSizer(dialogSizer)
dialogSizer.Add(panel,1,wx.EXPAND)
self.Layout()
panel.Layout()

(Also I think someone else mentioned that you should think about making more meaningful/apt names for objects. Calling a textCtrl “txtWidth” or a sizer hbox may add an extra layer of work for your brain that will make dealing with sizers all the harder. )

Che

On Fri, Jul 23, 2010 at 9:52 PM, C M cmpython@gmail.com wrote:

On Fri, Jul 23, 2010 at 9:14 PM, 音本四 onpon4@gmail.com wrote:

I’m still a beginner with sizers, so I’m sure this is just something simple I’m neglecting.

Here is my custom dialog class:

class SizeDialog(wx.Dialog):
def init(self, parent, id, title, defsize=(0,0)):

    wx.Dialog.__init__(self, parent, id, title, size=(300,120))

    panel = wx.Panel(self)

    vbox = wx.BoxSizer(wx.VERTICAL)
    hbox = wx.BoxSizer(wx.HORIZONTAL)
    buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)






    self.txtWidth = wx.TextCtrl(panel, -1, str(defsize[0]))
    label = wx.StaticText(panel, -1, 'X')
    self.txtHeight = wx.TextCtrl(panel, -1, str(defsize[1]))
    hbox.Add(self.txtWidth, 1, wx.ALIGN_CENTER_VERTICAL)





    hbox.Add(label, 1, wx.ALIGN_CENTER_VERTICAL)
    hbox.Add(self.txtHeight, 1, wx.ALIGN_CENTER_VERTICAL)

    vbox.Add(hbox, 1, wx.LEFT|wx.RIGHT|wx.EXPAND|wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)




    vbox.Add(buttons, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 10)


    panel.SetSizer(vbox)

It’s not working like I expected; everything is squished together in the corner (as if the sizers didn’t exist).

What happens if at the end you add:

panel.Layout()

Che


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Well, this was what I used to test it:

Really the best thing is to include the whole runnable chunk of code as an attachment. In order to get your code to run I had to copy and paste this part and then go back to the first message and get the rest of it. Not complaining, but it is just good policy to have the question asker provide an everything-included way to test someone’s problematic code.

if name == ‘main’:
app = wx.PySimpleApp()
dia = SizeDialog(None, -1, ‘test’, (16,24))

r = dia.ShowModal()
if r == wx.OK:

    print 'You pressed OK! Size: {0}x{1}'.format(dia.width.GetValue(),dia.height.GetValue())
elif r == wx.CANCEL:
    print 'You pressed Cancel!'
else:
    print 'You pressed... nothing.'


    print r
dia.Destroy()
app.MainLoop()

Adding the sizer did make the TextCtrls and StaticText visible more like they were supposed to be, but the buttons still aren’t visible. Also, I don’t quite understand why explicitly defining a sizer would be necessary… It wasn’t necessary on my frame class.

It wasn’t necessary on a frame because a frame automatically resizes the first panel on it to be the full size of the frame–dialogs don’t do that. (They sort of already are a frame with a panel on it, so to speak).

Try the attached code. The difference is I did away with your panel, since with this dialog you don’t need one, and that simplifies it and may be what you need.

No, no one mentioned anything to me about meaningful names. I was already pestered about that in Visual Basic class (heck, we even had to meaningfully name temporary loop variables!), and I have since dropped writing 20+ character long names in favor of shorter (more practical) names. Sure I could call the box something like szrTopHorizontalBox, but why use such a long and complicated name when I can just call it hbox?

Sorry it was to someone named Seb from Ray. It’s a personal choice of course, but my feeling is if you call something txtWidth it should be a width value and not actually a text control. hbox is not complicated but it is also not informative. It could be a box-like panel, it could be a StaticBox… It’s a sizer, so I’d at least get the word “sizer” in there, as in hboxsizer. For larger sections of code I might even put something like textCtrlsBoxSizer. The reason is when you later go back to revise/fix your code, it will be so much more readable and easier to work on when you compare “hbox” to “textCtrlsBoxSizer” (then again, I actually sort of liked the IUPAC chemicals naming conventions… :D)

Che

dialog_simpler.py (1.29 KB)

···

On Sat, Jul 24, 2010 at 12:18 AM, 音本四 onpon4@gmail.com wrote:

Thanks, I’ve finally got it working like I wanted it to.

···

On Sat, Jul 24, 2010 at 1:07 AM, C M cmpython@gmail.com wrote:

On Sat, Jul 24, 2010 at 12:18 AM, 音本四 onpon4@gmail.com wrote:

Well, this was what I used to test it:

Really the best thing is to include the whole runnable chunk of code as an attachment. In order to get your code to run I had to copy and paste this part and then go back to the first message and get the rest of it. Not complaining, but it is just good policy to have the question asker provide an everything-included way to test someone’s problematic code.

if name == ‘main’:
app = wx.PySimpleApp()

dia = SizeDialog(None, -1, 'test', (16,24))

r = dia.ShowModal()
if r == wx.OK:

    print 'You pressed OK! Size: {0}x{1}'.format(dia.width.GetValue(),dia.height.GetValue())
elif r == wx.CANCEL:
    print 'You pressed Cancel!'
else:
    print 'You pressed... nothing.'



    print r
dia.Destroy()
app.MainLoop()

Adding the sizer did make the TextCtrls and StaticText visible more like they were supposed to be, but the buttons still aren’t visible. Also, I don’t quite understand why explicitly defining a sizer would be necessary… It wasn’t necessary on my frame class.

It wasn’t necessary on a frame because a frame automatically resizes the first panel on it to be the full size of the frame–dialogs don’t do that. (They sort of already are a frame with a panel on it, so to speak).

Try the attached code. The difference is I did away with your panel, since with this dialog you don’t need one, and that simplifies it and may be what you need.

No, no one mentioned anything to me about meaningful names. I was already pestered about that in Visual Basic class (heck, we even had to meaningfully name temporary loop variables!), and I have since dropped writing 20+ character long names in favor of shorter (more practical) names. Sure I could call the box something like szrTopHorizontalBox, but why use such a long and complicated name when I can just call it hbox?

Sorry it was to someone named Seb from Ray. It’s a personal choice of course, but my feeling is if you call something txtWidth it should be a width value and not actually a text control. hbox is not complicated but it is also not informative. It could be a box-like panel, it could be a StaticBox… It’s a sizer, so I’d at least get the word “sizer” in there, as in hboxsizer. For larger sections of code I might even put something like textCtrlsBoxSizer. The reason is when you later go back to revise/fix your code, it will be so much more readable and easier to work on when you compare “hbox” to “textCtrlsBoxSizer” (then again, I actually sort of liked the IUPAC chemicals naming conventions… :D)

Che

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en