代码之家  ›  专栏  ›  技术社区  ›  fred basset

Python WxWidgets-如何使静态文本标签显示在渐变绘制面板的顶部

  •  1
  • fred basset  · 技术社区  · 13 年前

    我有一个WxWidgets应用程序。我正在使用Python。该应用程序有一个主框架,一个菜单栏,底部有一个状态栏,对于中间的主要内容,我想要一个渐变面板,以提供良好的背景着色效果。我已经完成了这项工作,但现在我想开始将GUI小部件添加到面板中(例如一组静态文本标签和编辑框)。如何将这些添加到面板中,使其显示在面板顶部?在我下面的示例代码中,向面板中添加一个文本字段会导致面板缩小。

    谢谢你的帮助。

    def createGui(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
        self.SetBackgroundColour("white")
        self.SetSizeHints(800, 640, 800, 600)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = wx.Panel(self, size=(800, 600))
        self.sizer.Add(self.panel)
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
    
        self.label1 = wx.StaticText(self, -1, "PV Power" , wx.Point(1, 1))
        # This next part is not working, I want to add static text at certain geometric locations to the panel, so I get to see
        # the panel with it's background gradient and the text labels on top
        #self.panel.AddChild(self.label1)
    
        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)
    
        self.Bind(wx.EVT_MENU, self.onQuit, fitem)
    
        # Create a box at the bottom for buttons
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText('Waiting to run')
        self.sizer.Add(self.statusbar)
    
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show()
    
    def on_paint(self, event):
        dc = wx.PaintDC(self.panel)
        x = 0
        y = 0
        w, h = self.GetSize()
        dc.GradientFillLinear((x, y, w, h), 'white', 'light grey')
    
    1 回复  |  直到 13 年前
        1
  •  1
  •   Joran Beasley    13 年前

    从你的问题来看,听起来你所需要做的就是

    self.panel.SetMinSize((800,600))
    

    不幸的是,在你创建面板后,wxStaticText的限制会引起很多麻烦(它将有一个白色背景(或者至少是彩色的))。你可以做一些技巧来将文本的背景设置为基本上模仿背景透明度的图像区域。。。但他们很痛苦

    推荐文章