Shane
Shane

Reputation: 4983

How do I make my wxpython top frame show in the middle of my desktop?

I want to make my GUI program top frame both vertically and horizontally aligned.

wx.Frame.__init__(self, parent=None, id= -1, title="Test Frame", pos=(-1, -1), size=(1280, 770), style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX)

What should I do(except do the calculation to find the absolute position) to pos=(-1, -1) to make it show in the middle(no matter 800 * 600 or 1280 * 800 etc.), or some other attributes setting also needed?

Upvotes: 9

Views: 7915

Answers (2)

TyrantWave
TyrantWave

Reputation: 4663

w = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
h = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)

pos=(w/2, h/2)

This gives you the centre of the screen.

Now, assuming you have an 800x600 sized application:

APPWIDTH = 800
APPHEIGHT = 600

w = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
h = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)

# Centre of the screen
x = w / 2
y = h / 2

# Minus application offset
x -= (APPWIDTH / 2)
y -= (APPHEIGHT / 2)

pos=(x, y)

Upvotes: 7

erbridge
erbridge

Reputation: 1386

Simply use

self.Center()

in the class __init__() instead of pos=(-1,-1).

Upvotes: 21

Related Questions