簡體   English   中英

如何在wxPython中居中放置wx.Grid

[英]How to center a wx.Grid in wxPython

我試圖居中wx.grid.Grid在wxPython中。 我嘗試了ID參數1,0,-1的所有可能組合。 我試圖將wx.ALIGN_CENTER_HORIZONTAL添加到許多定wx.ALIGN_CENTER_HORIZONTAL器。 我已經為此工作了幾天,我們將不勝感激。

我的問題是:“如何居中wxgrid?”

這是我的代碼:

main.py

import wx
from LogView import LogView

class MyFrame(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(850,725))

        self.main = wx.Panel(self)

        self.nb = wx.Notebook(self.main, 1)
        self.logView = LogView(parent=self.nb, ID=-1)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.nb.AddPage(self.logView, "Log")

        # Create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Adding Objects to mainSizer
        self.mainSizer.AddSpacer(10)
        self.mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)

        # Set main sizer
        self.main.SetAutoLayout(True)
        self.main.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.main)
        self.Layout()
        self.Centre(wx.BOTH)
        self.Show()

app = wx.App(False)
frame = MyFrame(None, -1, 'TFM UI')
app.MainLoop()

LogView.py

import wx
from GridTable import GridTable
class LogView(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.gridPnl = wx.Panel(self)
        self.gridPnlSizer = wx.BoxSizer(wx.VERTICAL)

        self.grid = GridTable(self.gridPnl)

        self.gridPnlSizer.AddStretchSpacer()
        self.gridPnlSizer.Add(self.grid,-1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self.gridPnlSizer.AddStretchSpacer()

        self.gridPnl.SetAutoLayout(True)
        self.gridPnl.SetSizer(self.gridPnlSizer)
        self.gridPnlSizer.Fit(self.gridPnl)

        self.sizer.AddSpacer(25)
        self.sizer.Add(self.gridPnl,-1,wx.EXPAND)


        self.SetAutoLayout(True)
        self.SetSizer(self.sizer)
        self.sizer.Fit(self)

GridTable.py

import wx
import wx.grid

class GridTable(wx.grid.Grid):
    def __init__(self, parent):
        #colDisplay should be a list

        wx.grid.Grid.__init__(self, parent)
        self.CreateGrid(100,3)
        for i in xrange(3):
            if i==0:
                self.SetColLabelValue(i, "Col"+str(i))
                self.SetColSize(i,85)
            else:
                self.SetColLabelValue(i, "Col"+str(i))
                self.SetColSize(i,150)


        for i in xrange(100):
            #clear grid
            self.SetRowLabelValue(i, str(i+1))
            for j in xrange(3):
                self.SetCellValue(i,j,"")
                if i==0:
                    self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                elif i%2==0:
                    self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                else:
                    self.SetCellTextColour(i, j, wx.BLACK)
                    self.SetCellBackgroundColour(i, j, wx.WHITE)

        for i in xrange(100):
            if i==0:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
            elif i%2==0:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
            else:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellTextColour(i, j, wx.BLACK)
                        self.SetCellBackgroundColour(i, j, wx.WHITE)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellTextColour(i, j, wx.BLACK)
                        self.SetCellBackgroundColour(i, j, wx.WHITE)

從LogView.py查看此行:

self.gridPnlSizer.Add(self.grid,-1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)

對於初學者,您使用的是-1的比例 ,我認為這是未定義的。 這會導致Python在我的機器上崩潰,所以...那不是很好。 默認值為0。

其次,您要告訴sizer擴展小部件。 我猜想如果小部件占用了所有可用空間,那么肯定以一個定義為中心,但是我認為那不是您的意思。

最后,拉伸墊片是不必要的。 因此,將以下三行代碼替換為:

self.gridPnlSizer.Add(self.grid,flag=wx.ALIGN_CENTER_HORIZONTAL)

當我這樣做時,我最終得到:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM