获取wxpython中按钮的位置

2024-06-16 11:09:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道wxpython中两个按钮的位置。我有一个gui,左边有一个小的垂直面板,右边有一个大面板。在

    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)

在右侧面板(笔记本)中,我使用垂直大小调整器堆叠三个面板:

^{pr2}$

我的窗格3包含三个使用gridsizer组织的按钮(一行三列)。一切看起来都很棒。现在,我希望能够得到三个按钮的屏幕位置(它们根据屏幕的分辨率而变化,用户可以调整gui的大小,等等)。在

我的屏幕大小是(19201080),它是从wx.GetDisplaySize()派生的。我试过了自我按钮1.GetScreenPosition()和自身窗格3.GetScreenPosition和自我按钮1.GetPosition()。第一个返回位置(77,93),第二个返回(61,95),最后一个返回(0,0)。在用testtext = wx.StaticText(self.Notebook3, -1, 'X marks spot',pos=(240,820))进行测试后,我发现我想要返回的按钮的位置是(240820)--大约。这是我要回的号码。在

有人知道我做错了什么吗?谢谢!在

*编辑*

我的代码应该是192080。在

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()

Tags: selfadd按钮expandwxoptpanelsplitter
1条回答
网友
1楼 · 发布于 2024-06-16 11:09:26

我觉得你要求这个职位太早了。您需要在所有内容呈现到屏幕后获取位置,因此可能需要使用wxPython的CallAfter。创建一个如下所示的方法:

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

然后在init末尾添加以下行:

^{2}$

当我在我的系统上运行它时,我得到了(155211),这可能更接近您所要查找的。我有个奇怪的决定。在

您可能还想看看这个线程,它讨论了滚动窗口的CalcUnscrolledPosition方法。在

相关问题 更多 >