wxPython 如何在状态栏中设置小部件位置 [英] wxPython how to set widget position inside statusbar

查看:17
本文介绍了wxPython 如何在状态栏中设置小部件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在状态栏中添加两个按钮,并按如下方式实现:

I would like to add two buttons inside the status bar, and I have them implemented as follows:

import wx

class MainFrame(wx.Frame):
    """Constructor"""

    def __init__(self, parent, id):
        wx.Frame.__init__(self, None)


        # self.status_bar = self.CreateStatusBar(3)
        self.status_bar = self.CreateStatusBar(3)

        self.status_bar.SetStatusText("some text", 0)

        self.button1 = wx.Button(self.status_bar, -1, "button 1")
        self.button2 = wx.Button(self.status_bar, -1, "button 2")
        self.status_bar.SetStatusWidths([-1, 200, 200])
        self.button1.SetPosition((self.status_bar.Size[0]-100, 0))
        self.button2.SetPosition((self.status_bar.Size[0]-200, 0))

        self.Refresh()
        self.Show()


if __name__ == "__main__":
    app = wx.App()
    MainFrame(None, -1)
    app.MainLoop()

问题是:我不知道如何正确设置按钮的位置.对于上面的示例,我已经为它们准备了两个插槽,但是我无法将它们放入插槽中.相反,我必须使用SetPosition"函数来设置按钮的固定位置,但是一旦窗口调整大小,按钮将留在那里并且不会被看到.

The problem is that: I do not know how to properly set the positions of the buttons. For the above example, I have two slots for them already, but I can't put them in the slots. In stead, I have to use the "SetPosition" function to set the fix positions of the buttons, but once the window gets resized the button will stay there and will not be seen.

我想知道是否有一种简单的方法来设置状态栏内按钮的位置,就像我们使用self.status_bar.SetStatusText(self.button1, 1)"和self.status_bar.SetStatusText(self.button2, 2)"

I was wondering if there is a easy way to set the position of the buttons inside the statusbar like we set the text using "self.status_bar.SetStatusText(self.button1, 1)" and "self.status_bar.SetStatusText(self.button2, 2)"

非常感谢!

==============================================================================

===========================================================================

非常感谢 Mike Driscoll 的回答,我已经找到了解决方法.如果有人需要,我会发布我的解决方案如下.

Thanks a lot to Mike Driscoll's answer, I have figured out the way to do it. I'll post my solution as follows in case any one needs it.

import wx

class MyStatusBar(wx.StatusBar):
    def __init__(self, parent):
        wx.StatusBar.__init__(self, parent)

        self.SetFieldsCount(3)
        self.SetStatusWidths([-1, 200, 200])
        self.sizeChanged = False
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_IDLE, self.OnIdle)

        # Field 0 ... just text
        self.SetStatusText("some text", 0)

        # Field for buttons
        self.button1 = wx.Button(self, 1001, "button 1")
        self.button2 = wx.Button(self, 1002, "button 2")

        # set the initial position of the checkbox
        self.Reposition()

    def OnSize(self, evt):
        evt.Skip()
        self.Reposition()  # for normal size events

        # Set a flag so the idle time handler will also do the repositioning.
        # It is done this way to get around a buglet where GetFieldRect is not
        # accurate during the EVT_SIZE resulting from a frame maximize.
        self.sizeChanged = True


    def OnIdle(self, evt):
        if self.sizeChanged:
            self.Reposition()


    # reposition the buttons
    def Reposition(self):
        rect1 = self.GetFieldRect(1)
        rect1.x += 1
        rect1.y += 1
        self.button1.SetRect(rect1)

        rect2 = self.GetFieldRect(2)
        rect2.x += 1
        rect2.y += 1
        self.button2.SetRect(rect2)

        self.sizeChanged = False


class MainFrame(wx.Frame):
    """Constructor"""

    def __init__(self, parent, id):
        wx.Frame.__init__(self, None)

        self.status_bar = MyStatusBar(self)
        self.SetStatusBar(self.status_bar)

        self.Refresh()
        self.Show()


if __name__ == "__main__":
    app = wx.App()
    MainFrame(None, -1)
    app.MainLoop()

推荐答案

最简单的方法是子类 wx.StatusBar 本身,添加按钮,然后捕获 wx.EVT_SIZE.通过这种方式,您可以捕获调整大小事件并可以根据需要重新定位小部件.

The easiest way would be to subclass wx.StatusBar itself, add the buttons and then catch the wx.EVT_SIZE. This way you catch resize events and can reposition the widgets as needed.

wxPython 演示有一个例子,它有一个复选框部件,方法如下:

The wxPython demo has an example where it has a checkbox widget with the following method:

# reposition the checkbox
def Reposition(self):
    rect = self.GetFieldRect(1)
    rect.x += 1
    rect.y += 1
    self.cb.SetRect(rect)
    self.sizeChanged = False

self.GetFieldRect(1) 指的是复选框小部件.除了需要跟踪两个小部件的位置并进行相应更新之外,您可以使用类似的东西.

The self.GetFieldRect(1) refers to the checkbox widget. You can use something like this for yours except that you will need to keep track of two widget's positions and update accordingly.

这篇关于wxPython 如何在状态栏中设置小部件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆