Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two steps you need to implement to solve your problem.</p> <p>The first step is to calculate how wide your string (you give "WWW" as an example) will be. Please take a look at <a href="https://stackoverflow.com/questions/2455255/how-to-get-the-width-of-a-string-in-pixels">this SO question about computing string widhs in pixels using wxpython</a>. Once you have the width and height, you can set your <code>wx.TextCtrl</code> sizes.</p> <p>The second step is setting the <code>wx.Frame</code> size. I want to start be saying that in wxpython terminology any object that can appear on screen is called a window and what most users would call a window (i.e. the thing with the minimize/maximize/close buttons in MS Windows) is called a frame. It can get confusing, I know.</p> <p>So you want to automatically set the size of your frame using the size of the contents. Easy! You want to use the <code>Fit()</code> method (<a href="http://wxpython.org/docs/api/wx.Window-class.html#Fit" rel="nofollow noreferrer">documentation</a>). You could also use the <code>SetSizerAndFit()</code> method (<a href="http://wxpython.org/docs/api/wx.Window-class.html#SetSizerAndFit" rel="nofollow noreferrer">documentation</a>) but this is just a convenience method that calls both <code>SetSizer()</code> and <code>Fit()</code> at the same time. The <code>Fit()</code> method takes a look at the size of the contents of an object and sizes that object accordingly. Unfortunately, it's not quite that simple in your particular code.</p> <p>In your code you have a <code>MyForm</code> (a <code>wx.Frame</code> instance) that contains a <code>wx.Panel</code> which then contains your sizers and your <code>wx.TextCtrl</code>s. Unfortunately, this causes problems computing the <code>MyForm</code>'s size based on the contents so calling <code>Fit()</code> will just set it to a default size. The reason is that <code>wx.Frame</code>s compute their size based on their sizers (as assigned by <code>SetSizer()</code>) and current <code>MyForm</code> does not have a sizer, so it cannot calculate the size based on the contents of your <code>wx.Panel</code>. Luckily there are two solutions to this problem and they're both pretty easy.</p> <p><strong>1. Remove <code>panel</code> and set everything as a child of the <code>MyForm</code> directly then call <code>SetSizerAndFit()</code></strong></p> <p>This would look something like this:</p> <pre><code>def init_UI(self): [...] #note that the parent is now "self" and not "panel" self.multiplicand = wx.TextCtrl(self, **text_ctrl_args) self.sum_result = wx.TextCtrl(self, style = wx.TE_READONLY, **text_ctrl_args) self.mult_result = wx.TextCtrl(self, style = wx.TE_READONLY, **text_ctrl_args) [...] self.SetSizerAndFit(hbox) </code></pre> <p><br> <strong>2. Place <code>panel</code> inside of another sizer and call <code>SetSizerAndFit()</code> on the <code>MyForm</code></strong></p> <p>Which would look something like this:</p> <pre><code>def init_UI(self): [...] panel.SetSizer(hbox) sizer = wx.BoxSizer(wx.Horizontal) sizer.Add(panel) self.SetSizerAndFit(sizer) </code></pre> <p><br> Finally, I want to offer a quick explanation as to why tutorials tend to use those "magic numbers". UI Design is difficult and sometimes you need things to be pixel perfect. If you're developing an app to be used on different computers with different settings and screen resolutions and even different OSes, ensuring everything is displayed consistently requires explicitly setting things like fonts and sizes and not relying on system defaults. As you progress and start making more complex wxpython applications you'll run into problems where sometimes the size of an object isn't calculated properly and using the methods I've told you to use will still make your window too big/small. Just keep this in mind moving forward.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload