如何在wxPython中使用StaticBox创建方形LED形状

时间:2019-09-04 07:32:09

标签: python wxpython

我正在尝试在GUI中创建LED形状。

我的示例代码是这个。

import wx

class Main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title ="Static box test")
        panel = wx.Panel(self)
        self.myLED = wx.StaticBox(panel, -1, "myLED", pos = (50,50), size = (100,100))
        self.myLED.SetBackgroundColour("blue")
        self.myLED.SetForegroundColour("white")

if __name__ == "__main__":
    app = wx.App()
    frame = Main()
    frame.Show()
    app.MainLoop()

GUI看起来像这样。

enter image description here

我想创建一个方形LED,在方形的中心有一个标签。

但是LED看起来不像一个完美的正方形,并且标签在左上角。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

在此示例中,使用 wx.Panel 表示LED,使用 wx.StaticText 表示文本。答案在于使用上浆器。

文档:https://wxpython.org/Phoenix/docs/html/sizers_overview.html

ShowSignoutPrompt

这就是您运行它会得到的

Example image

希望这可以为您提供帮助。