質問

I am trying to figure out the position of two buttons in wxpython. I have a gui with a small vertical panel on the left and large panel on the right.

    wx.Frame.__init__(self, *args, **kwds)

    self.Splitter = wx.SplitterWindow(self, -1)#, style=wx.SP_NOSASH)

    self.Panel1 = wx.Panel(self.Splitter, -1)          
    self.Panel3 = wx.Panel(self.Splitter, -1)

    self.Splitter.SplitVertically(self.Panel1,self.Panel3,350)

In in the right panel (a notebook) I use a Vertical Sizer to stack three panels:

    self.Notebook3 = wx.Notebook(self.Panel3, -1)
    self.OptPane = scrolled.ScrolledPanel(self.Notebook3, -1)

    self.pane1 = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
    self.pane2 = wx.Panel(self.OptPane,-1, style=wx.RAISED_BORDER)
    self.pane3= wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)

My pane 3 contains three buttons that are organized using a gridsizer (one row, three columns). It all looks great. Now, I want to be able to get the screen position of the three buttons (they change based on resolution of screen, person adjusting the size of the gui, etc.).

My screen size is (1920,1080) which is derived from wx.GetDisplaySize(). I have tried self.button1.GetScreenPosition() and self.pane3.GetScreenPosition and self.button1.GetPosition(). The first returns the position (77,93), second returns (61,95) and the last one gives me (0,0). After testing with testtext = wx.StaticText(self.Notebook3, -1, 'X marks spot',pos=(240,820)), I figured out the position of the button I want returned is (240,820) -- approximately. This is the number I want to return.

Does anyone know what I am doing wrong? Thanks!

*EDIT*

My Full Code - should be runnable -- I am running this on a 1920x1080 (for the text 'x marks the spot').

import wx
import wx.lib.scrolledpanel as scrolled

class TMainForm(wx.Frame):

    def __init__(self, *args, **kwds):

        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        self.Splitter = wx.SplitterWindow(self, -1)

        self.Panel1 = wx.Panel(self.Splitter, -1)          
        self.Panel3 = wx.Panel(self.Splitter, -1)

        self.Splitter.SplitVertically(self.Panel1,self.Panel3,350)

        self.Notebook2 = wx.Notebook(self.Panel1, -1)

        self.Notebook3 = wx.Notebook(self.Panel3, -1)
        self.OptPane = scrolled.ScrolledPanel(self.Notebook3, -1)
        self.OptPane.SetupScrolling()

        self.Opt_Info = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)
        self.Opt_Middle = wx.Panel(self.OptPane,-1, style=wx.RAISED_BORDER)
        self.Opt_Buttons = wx.Panel(self.OptPane,-1, style=wx.NO_BORDER)

        self.Button1 = wx.Button(self.Opt_Buttons,-1,'Button1',size=(-1,-1))
        self.Button2 = wx.Button(self.Opt_Buttons,-1,'Button2',size=(-1,-1))
        self.Button3 = wx.Button(self.Opt_Buttons,-1,'Button3',size=(-1,-1))

        self.MainMenu = wx.MenuBar()
        self.FileMenu = wx.Menu()
        self.FileOpenItem = wx.MenuItem(self.FileMenu, 103, "&Open\tCtrl+O", "Open a Previous Session", wx.ITEM_NORMAL)
        self.FileMenu.AppendItem(self.FileOpenItem)
        self.FileSaveItem = wx.MenuItem(self.FileMenu, 102, "&Save\tCtrl+S", "Save the data", wx.ITEM_NORMAL)
        self.FileMenu.AppendItem(self.FileSaveItem)
        self.FileQuitItem = wx.MenuItem(self.FileMenu, wx.ID_EXIT, "&Quit\tCtrl+Q", "Quit the program", wx.ITEM_NORMAL)
        self.FileMenu.AppendItem(self.FileQuitItem)  
        self.MainMenu.Append(self.FileMenu, "&File")
        self.SetMenuBar(self.MainMenu)

        self.__set_properties()
        self.__do_layout()

        print self.Button1.GetScreenPosition()
        testtext = wx.StaticText(self.Notebook3, -1, 'X marks spot',pos=(240,840))

    def __set_properties(self): 

        self.SetTitle("My Program")

        screen_x = 95 * wx.GetDisplaySize()[0]/100
        screen_y = 90 * wx.GetDisplaySize()[1]/100
        self.SetSize((screen_x, screen_y))
        self.SetFocus()

    def __do_layout(self , call_fit = True, set_sizer = True): 

        vbox  = wx.BoxSizer(wx.VERTICAL)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        hbox1.Add(self.Opt_Info, 1, wx.EXPAND|wx.ALL, 3)
        hbox2.Add(self.Opt_Middle, 1, wx.EXPAND|wx.ALL, 3)
        hbox3.Add(self.Opt_Buttons, 1, wx.EXPAND|wx.ALL, 3)

        box_bot = wx.GridSizer(1,3,2,2)

        box_bot.Add(self.Button1, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)    
        box_bot.Add(self.Button2, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)
        box_bot.Add(self.Button3, 1, wx.ALIGN_CENTER| wx.LEFT | wx.RIGHT, 55)

        self.Opt_Buttons.SetSizer(box_bot)

        vbox.Add(hbox1, 0, wx.EXPAND|wx.TOP, 20)
        vbox.Add(hbox2, 1, wx.EXPAND|wx.TOP, 50)
        vbox.Add(hbox3, 0, wx.EXPAND|wx.ALL, 20)
        self.OptPane.SetSizer(vbox)

        self.Notebook3.AddPage(self.OptPane,"Page1")

        #Sizer for Panel 2
        sizer_P2 = wx.BoxSizer(wx.VERTICAL)
        sizer_P2.Add(self.Notebook2, 1, wx.EXPAND, 0)
        self.Panel1.SetSizer(sizer_P2)

        #Sizer for Panel 3
        sizer_P3 = wx.BoxSizer(wx.VERTICAL)
        sizer_P3.Add(self.Notebook3, 1, wx.EXPAND, 0)
        self.Panel3.SetSizer(sizer_P3)

        # Split Panel Sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.Splitter,1,wx.EXPAND)
        self.SetSizer(sizer)

        self.Layout()
        self.Centre()

# Code to Execute File
class TApplication(wx.App):
    def OnInit(self):

            wx.InitAllImageHandlers()
            MainForm = TMainForm(None, -1,"")
            self.SetTopWindow(MainForm)

            MainForm.Show()
            return 1

if __name__ == "__main__":
    Application = TApplication(0)
    Application.MainLoop()
役に立ちましたか?

解決

I think you're asking for the position too soon. You'll want to get the position after everything is rendered to the screen, so you'll probably need to use wxPython's CallAfter. Create a method that looks something like this:

def printLocation(self):
    """"""
    print self.Button1.GetScreenPosition()

Then at the end of your init, add the following line:

wx.CallAfter(self.printLocation)

When I ran it on my system, I got (155, 211), which is probably closer to what you're looking for. I have a rather weird resolution here.

You might also want to look at this thread which talks about the scrolled window's CalcUnscrolledPosition method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top