Hussein Esmail
Hussein Esmail

Reputation: 363

Getting window size after the user has altered it in wxPython

I'm trying to find a way to get the horizontal length of a window, just in case the user has altered it, because if I do not do this, the text that I need to show sometimes cuts off. I am using python 3.6.1 on macOS, with the wx (wxPython) library.

Upvotes: 1

Views: 4033

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

Use GetSize() for the widget that you are interested in.
It returns (Width,Height).
Run this code and resize the window.

import wx

def size_change(event):
    width,height = event.GetSize()
    print ("Width =",width,"Height =",height)

app = wx.App()
frame = wx.Frame(None, -1, 'Get Size Test')
frame.SetSize(500,250)
frame.Bind(wx.EVT_SIZE, size_change)
frame.Show()

print ("Original Size =",frame.GetSize())

app.MainLoop()

Upvotes: 1

Nae
Nae

Reputation: 15325

Assuming that you mean width, with horizontal length you can use:

widget.size

for a widget's size and; widget.Size[0] for width and widget.Size[1] for height specifically.

Also based on this you can limit set a minimum size for the widget as well so that the user can't resize it less than that:

widget.SetMinSize(wx.Size(320,240))

Here's an example code:

import wx

root = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,320,480)
frame.SetMinSize(wx.Size(320,240))
frame.Show()

print(frame.Size[0])
root.MainLoop()

Upvotes: 2

Related Questions