2017-10-01 21:51:58

hi all.
well, i'd like to ask something about wxpython
how do you guys handle the screen positions? i mean, how do you know the exactly positioning of the windows on the screen? if for example i'd like to put a label how can i know exactly the position on wich i have to put it?
and the thing complicates when you have to think about other screen risolutions.
any tips?
thanks

Paul

2017-10-01 22:30:28 (edited by Ethin 2017-10-01 22:31:58)

This all revolves around screen pixelations; specifically the position of your label in terms of pixels. I know you can set it in C++ by calling (I think) wxStaticText::wxStaticText::wxStaticText (the constructor) that has the signature of:

wxStaticText::wxStaticText  (wxWindow * parent, wxWindowID id, const wxString &label, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, long style = 0, const wxString &name = wxStaticTextNameStr)

. Not sure what that translates to in Python. I haven't used WXWidgets for anything major -- compiling any program in C++ with it is a bitch (anyone know how to fix that -- things like "cannot find setup.h" and that kind of thing?) but if I ever start fully working with it I'll tell you how it goes if I run into something like your running into now.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2017-10-02 02:36:04 (edited by magurp244 2017-10-02 06:44:30)

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

2017-10-02 10:39:35

yes, i was meaning the position of the frame on the windows.
thanks a lot for the exaustive reply, now i've understood clearly than before.

Paul