Are you referring to positioning the program window on the desktop? Or are you referring to positioning objects inside the program window, frame, or panels? There are a few functions available that allow you to get the dimensions and positions of the windows, frames, and panels that your using. For the main window sitting on the desktop it would be "GetPosition()" which returns a tuple with the x/y coordinates, and to set the windows position you can use "SetPosition((x,y))", the desktops current resolution being your working area. For reference in wxPython (0,0) always refers to the upper left corner, so a higher X value would move text, buttons, or images further right, and a higher Y value moves them further down.
Positioning objects and text inside panels is quite similar, where you can also use GetPosition and SetPosition to place them. But to figure that out you need to know what the width and height of the panel or frame is to place it in, and what the width and height of the text or button is. To do this you can use "GetClientSize()" to find the desired frames or panels dimensions, and for text and buttons you can use "GetSize()".
Now lets say you have a window thats 320 by 240 and you have a button thats 75 by 23, and you wanted to place the button roughly in the middle of the window. You'd take the width of the window and the button and divide them by two and subtract them, 120 - 12 (roughly) = 108, which is the middle of the window. Now if you wanted to put the button at the bottom of the screen, you'd take the height of the window and subtract the height of the button to use as the buttons Y position, as otherwise it would disappear off the bottom of the window!
Now you can also make it so the window can't be resized to not have to deal with different resolutions too much, but another way would be to use Sizers which automatically scale and position panels and frames. We can get into that further if you like, but for now an example:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Example", pos=(150,150), size=(350,200),style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
self.Bind(wx.EVT_CLOSE, self.OnClose)
print "The width and height of the frame is:", self.GetClientSize()
#Get the current position of the window on the desktop
print self.GetPosition()
#Set the windows current position on the desktop
self.SetPosition((0,0))
#create panel in frame
self.panel = wx.Panel(self)
#create Hello World! text string within the panel
m_text = wx.StaticText(self.panel, -1, "Hello World!")
#set string font
m_text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
#position the text string in the panel
m_text.SetPosition((0,23))
print "The texts width and height is:", m_text.GetSize()
#create a close button
m_close = wx.Button(self.panel, wx.ID_CLOSE, "Close")
#Bind the close button to trigger the OnClose() function when pressed
m_close.Bind(wx.EVT_BUTTON, self.OnClose)
print "The buttons width and height is:", m_close.GetSize()
#expand the panel to its proper size inside the frame
self.panel.Layout()
print "The Panels width and height is:", self.panel.GetClientSize()
#open a close window dialog
def OnClose(self, event):
dlg = wx.MessageDialog(self,
"Do you really want to close this application?",
"Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
app = wx.App()
top = Frame()
top.Show()
app.MainLoop()
-
BrushTone v1.3.3: Accessible Paint Tool
-
AudiMesh3D v1.0.0: Accessible 3D Model Viewer