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

Hub*_*bro 5 python wxpython

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

| |
| |
================================================== ================================
| 一些状态文本。v. 1.0.32 |

我目前的代码:

self.CreateStatusBar(2)
self.SetStatusWidths([-1, -1])
Run Code Online (Sandbox Code Playgroud)

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

| |
| |
================================================== ================================
| 一些状态文本。v. 1.0.32 |

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

Hub*_*bro 5

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

Windows 7的:

Windows 7的

OSX 10.8.1 山狮:

操作系统X

下面是自定义状态栏的代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

self.statusbar = CustomStatusBar(self)
self.SetStatusBar(self.statusbar)
Run Code Online (Sandbox Code Playgroud)

我将此功能添加到我的框架中,以便轻松更新状态:

def set_status_text(text):
    self.statusbar.SetStatusText(text, 0)
Run Code Online (Sandbox Code Playgroud)

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


Sta*_*kes 5

这个 hacky 版本并不完全符合我的要求。我发现根据字符串的像素大小设置静态宽度大小对我来说非常有用。能很好地适应环境并可以动态改变。这是使用 Python 3 和 wxPython Phoenix 完成的,所以也许这个方法以前不可用。

这是一个简单的版本。

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        version = "v. 1.0.32 (2012-11-03)"
        # Returns a Size object of the pixel dimensions of a string
        version_size = wx.Window.GetTextExtent(self, version)

        # Create a status bar with two sections
        self.CreateStatusBar(2)
        # Set the left side to a negative number indicating it's fluid width
        # The right side will be the exact size of the version string
        self.SetStatusWidths([-1, version_size.width])
        # Set the left side of the status bar to some text. 0 is first section
        self.SetStatusText("left status bar text", 0)
        # Set right side to version number.  1 is second section
        self.SetStatusText(version, 1)

        self.Show()

app = wx.App()
frame = MainWindow(None)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)

为了展示如何动态更新它,这里是一个带有线程随机更新的更复杂的版本。

import wx
import time
import random
import threading

class MainWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        version = "v. 1.0.32 (2012-11-03)"
        # Returns a Size object of the pixel dimensions of a string
        version_size = wx.Window.GetTextExtent(self, version)

        # Create a status bar with two section
        self.CreateStatusBar(2)
        # Set the left side to a negative number indicating it's fluid width
        # The right side will be the exact size of the version string
        self.SetStatusWidths([-1, version_size.width])
        # Set the left side of the status bar to some text. 0 is first section
        self.SetStatusText("left status bar text", 0)
        # Set right side to version number.  1 is second section
        self.SetStatusText(version, 1)

        self.Show()

        # Thread to update status bar with causing the GUI to hang
        run_version_updates = threading.Thread(target=self.thread_version_updating)
        run_version_updates.start()

    def thread_version_updating(self):
        for i in range(10):
            time.sleep(1)
            # Create a random string of 1-20 characters containing only "ABC123"
            random_string = "".join(random.choice("ABC123") for _ in range(random.randrange(1,20)))
            self.update_version(random_string)

    def update_version(self, version):
        # Get width of string, set status bar width, then update the text
        size = wx.Window.GetTextExtent(self, version)
        self.SetStatusWidths([-1, size.width])
        self.SetStatusText(version, 1)

app = wx.App()
frame = MainWindow(None)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)

我只有一种操作系统,但以下是不同 Windows 7 主题的外观。

赢7航空
在此输入图像描述

赢7经典
在此输入图像描述