我想要一个包含2个字段的状态栏。第一个左对齐,第二个右对齐。我想要的插图:
| | | | ================================================================================= | Some status text. v. 1.0.32 |
我目前的代码:
self.CreateStatusBar(2)
self.SetStatusWidths([-1, -1])
但是右边的字段是左对齐的,所以看起来像这样:
| | | | ================================================================================= | Some status text. v. 1.0.32 |
有没有办法让正确的字段中的文字与右边对齐?
答案 0 :(得分:4)
好的,我已经解决了这个问题(尽管感觉就像是黑客。)我创建了一个定义两个字段的自定义工具栏。左侧字段可以像平常一样进行控制,右侧字段包含StaticText
控件,其中包含手动定位的版本号。用于定位文本的代码是特定于平台的,因为它在窗口上看起来有点不同。以下是一些截图:
Windows 7:
OSX 10.8.1 Mountain Lion:
以下是自定义状态栏的代码:
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)
我希望这可以帮助其他人。
答案 1 :(得分:3)
您可以尝试使用制表符:
self.SetStatusText(0,"\tCentered")
self.SetStatusText(1,"\t\tRight Aligned")
这适用于Windows,但我不确定它是否在其他wxWidgets发行版中实现。您需要使用style=0
创建wx.StatusBar以禁用右下角的抓握。文本对齐不会考虑到夹点,它会被切断。如果这不起作用,您可以通过将右侧面板设置为静态大小来模拟正确的文本对齐:
self.SetStatusWidths([-1, 100])
但其上的文字将保持左对齐。要使文本看起来正确对齐,您必须用空格填充文本。请记住,不同的系统和用户可能使用不同的字体,因此间距可能不准确。
附录:将文本与\t
对齐实际上是本机Windows状态栏控件的一项功能。为了使其能够在其他操作系统上运行,它们的本机控件或wxWidgets都必须实现该行为。
答案 2 :(得分:1)
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()
为了展示如何动态更新它,这是一个带有线程随机更新的更复杂的版本。
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()
我只有一个操作系统,但这里有不同的Windows 7主题。
赢得7 Aero