I would like to know how to use the wx.gridsizer to set the text background setting to one color

时间:2017-08-19 01:16:43

标签: wxpython

There are four color backgrounds attached. I set the background using wx.gridsizer so background applies only to text size. I would like to have a one line color line as an attached image. How do I do this?

Must be sorted by wx.gridsizer.

import wx
class test(wx.Frame):
   def __init__(self):
      wx.Frame.__init__(self, None)
      main = wx.Panel(self, style=wx.DEFAULT)

      a=wx.StaticText(main, label='111111111')
      a.SetBackgroundColour('red')
      b=wx.StaticText(main, label='222222222')
      b.SetBackgroundColour('blue')
      c=wx.StaticText(main, label='333333333')
      c.SetBackgroundColour('yellow')
      d=wx.StaticText(main, label='444444444')
      d.SetBackgroundColour('cyan')

      sizer = wx.GridSizer(0,4,10,10)
      sizers = (a,b,c,d)

      for i in sizers:
         sizer.Add(i, 1, wx.ALL|wx.EXPAND, 1)
      vbox = wx.BoxSizer(wx.VERTICAL)
      vbox.Add(sizer, wx.SUNKEN_BORDER)
      main.SetSizer(vbox)

if __name__ == '__main__':
    app = wx.App()
    frame = test()
    frame.Show(True)
    app.MainLoop()

enter image description here

查看滚动到的位置

1 个答案:

答案 0 :(得分:2)

尺寸,尺寸,他们没有设置背景颜色的方法 您的问题是,您在sizer horizontal gap中的项目之间添加了sizer = wx.GridSizer(0,4,10,10) 将其更改为sizer = wx.GridSizer(0,4,10,0)
wxPython支持的一些平台(最值得注意的是GTK)不会将wx.StaticText视为单独的小部件;相反,标签只是直接在其父窗口上绘制。这些平台不允许开发人员更改窗口小部件的背景颜色 使用 GenStaticText 将克服上述所有问题,因为它是一个通用的小部件和一个真正的窗口。

import wx
import wx.lib.stattext as ST
class test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        main = wx.Panel(self,-1)
        a=ST.GenStaticText(main, label='111111111', size=(100,-1))
        b=ST.GenStaticText(main, label='222222222', size=(100,-1))
        c=ST.GenStaticText(main, label='3333', size=(100,-1))
        d=ST.GenStaticText(main, label='444444444', size=(100,-1))
        sizer = wx.GridSizer(0,4,10,0)
        sizers = (a,b,c,d)
        for i in sizers:
            i.SetBackgroundColour(wx.RED)
            sizer.Add(i, 1, wx.ALL|wx.EXPAND)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(sizer, wx.SUNKEN_BORDER)
        main.SetSizer(vbox)

if __name__ == '__main__':
    app = wx.App()
    frame = test()
    frame.Show(True)
    app.MainLoop()

请注意,我已经给每个静态文本size这确保了文本的长度不会覆盖sizer做出的决定并保持一致。

enter image description here