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 航空
赢 7 经典