python - 如何在 wxPython 中将 StatusBar 中的文本右对齐?

标签 python wxpython

我想实现一个有 2 个字段的状态栏。第一个左对齐,第二个右对齐。我想要的插图:

|                                                                               |
|                                                                               |
=================================================================================
| Some status text.                                                   v. 1.0.32 |

My current code:

self.CreateStatusBar(2)
self.SetStatusWidths([-1, -1])

但是右边的字段是左对齐的,所以看起来是这样的:

|                                                                               |
|                                                                               |
=================================================================================
| Some status text.                      v. 1.0.32                              |

有什么方法可以让正确字段中的文本右对齐?

最佳答案

好的,我已经解决了这个问题(尽管它感觉像是一个 hack。)我创建了一个定义两个字段的自定义工具栏。左边的字段可以正常控制,右边的字段包含一个 StaticText 控件,其中包含手动定位的版本号。用于定位文本的代码是特定于平台的,因为它在 Windows 上看起来有点不同。以下是一些屏幕截图:

window 7:

Windows 7

OSX 10.8.1 山狮:

OSX

这是自定义状态栏的代码:

class CustomStatusBar(wx.StatusBar):
    """A custom status bar for displaying the application version in the bottom
    right corner."""
    def __init__(self, parent):
        wx.StatusBar.__init__(self, parent, -1)

        self.SetFieldsCount(2)
        self.SetStatusWidths([-1, -1])

        # Set up the version label.
        self.version_label = wx.StaticText(self, -1, 'Version: ' + VERSION)
        self.reposition_version_label()

        # Listen to the resize event.
        self.Bind(wx.EVT_SIZE, self.on_resize)

    def on_resize(self, event):
        self.reposition_version_label()

    def reposition_version_label(self):
        # Get the rect of the second field.
        field_rect = self.GetFieldRect(1)
        label_rect = self.version_label.GetRect()

        # Reduce the width of the field rect to the width of the label rect and
        # increase it's x value by the same about. This will result in it being
        # right aligned.
        width_diff = field_rect.width - label_rect.width

        field_rect.width = label_rect.width
        field_rect.x += width_diff

        # On windows, the text is a little too high up, so increase the Y value
        # a little.
        if sys.platform == 'win32':
            field_rect.y += 3

        # Set the resulting rect to the label.
        self.version_label.SetRect(field_rect)

这段代码在我的 Frame 的构造函数中,用于创建和放置状态栏:

self.statusbar = CustomStatusBar(self)
self.SetStatusBar(self.statusbar)

我将这个功能添加到我的框架中以便于更新状态:

def set_status_text(text):
    self.statusbar.SetStatusText(text, 0)

我希望这对其他人有帮助。

关于python - 如何在 wxPython 中将 StatusBar 中的文本右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210822/

相关文章:

python - 是否可以在 wxPython 应用程序中使用 Panda3D?

gtk - wxpython 保存文件对话框给出 gtk 警告

Python:在 "If"循环中嵌套 "while"语句?

python - 在 python xmpp 中检索 gtalk 昵称

wxPython:wx.Panel 和 wx.Window 有什么区别?

python - 在wxpython listctrl中创建删除线

python - PhantomJS 可以与 py2exe 一起使用吗?

python - 在 Django 项目中包含 ffmpeg

python - 自动调整自定义 kivy 小部件的大小

python - 元类可以在类的范围内操作裸露的表达式吗?