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主題。
的Win 7的Aero
Win 7的經典
+1我設法完成我試圖使用自定義狀態欄做。謝謝你的提示。儘管我可能會接受我自己的答案,因爲它詳細描述了我最終做了什麼。 – Hubro