wxpython listctrl并修复列宽

时间:2013-08-09 18:51:22

标签: python wxpython width multiple-columns listctrl

我试图在我的面板中使“桌子”看起来很好。我遇到的这个问题是桌子填满了面板,但一切都在左边。我希望每列的大小和面板的整个宽度都相同。我见过ListCtrlAutoWidthMixin,但我不确定如何利用它。以下是我在代码中的内容:

        self.Analysis = scrolled.ScrolledPanel(self.Notebook3, -1)

        # Set Up Analysis Page (Page has four panels -- I care about Top left one)
        self.AnalysisUL = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
        self.AnalysisUR = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
        self.AnalysisLL = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
        self.AnalysisLR = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)

        # Top Left Box (Analyze Button and Projected Points Total)
        self.Picks_Left = wx.ListCtrl(self.AnalysisUL,-1,style=wx.LC_REPORT | wx.BORDER_NONE)
        self.Picks_Left.InsertColumn(1,'col1')
        self.Picks_Left.InsertColumn(2,'col2',format=wx.LIST_FORMAT_CENTRE)
        self.Picks_Left.InsertColumn(3,'col3',format=wx.LIST_FORMAT_CENTRE)
        self.Picks_Left.InsertColumn(4,'col4',format=wx.LIST_FORMAT_CENTRE)

        self.Picks_Left.SetColumnWidth(0,-2)
        self.Picks_Left.SetColumnWidth(1,-2)
        self.Picks_Left.SetColumnWidth(2,-2)
        self.Picks_Left.SetColumnWidth(3,-2)

        # Sizer
        vbox_UL = wx.BoxSizer(wx.VERTICAL)

        #Title for Table
        fontUL = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        self.UL_text = wx.StaticText(self.AnalysisUL, -1, 'Title')
        self.UL_text.SetFont(fontUL)

        vbox_UL.Add(self.UL_text, 0, wx.CENTER|wx.ALL,20)
        vbox_UL.Add(self.Picks_Left,1,wx.EXPAND|wx.ALL,3)
        self.AnalysisUL.SetSizer(vbox_UL)

同样,表格跨越了面板,但我的所有列都向左移动。另外,我不想手动设置列大小(除非我可以抓住面板大小然后除以我的列数 - 因为程序将在具有不同分辨率的计算机上使用)。我尝试过这种方法,但似乎无法获得正确的面板尺寸。我用GetSize()和GetClientSize()获得的数字都太小了。

感谢您的帮助!

编辑:已添加代码:

import wx
import wx.lib.mixins.listctrl as listmix
import wx.lib.scrolledpanel as scrolled
import wx.lib.agw.pybusyinfo as PBI

class TMainForm(wx.Frame):

    def __init__(self, *args, **kwds):


            kwds["style"] = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
            wx.Frame.__init__(self, *args, **kwds)

            self.Splitter1 = wx.SplitterWindow(self, -1)#, style=wx.SP_NOSASH)
            self.Splitter2 = wx.SplitterWindow(self.Splitter1)

            self.Panel1 = wx.Panel(self.Splitter2, -1)
            self.Panel2 = wx.Panel(self.Splitter2, -1)

            self.Splitter2.SplitHorizontally(self.Panel1,self.Panel2)

            self.Panel3 = wx.Panel(self.Splitter1, -1)
            self.Splitter1.SplitVertically(self.Splitter2,self.Panel3,400)

            self.Notebook = wx.Notebook(self.Panel1, -1)
            self.MyTeam = scrolled.ScrolledPanel(self.Notebook, -1)
            self.TeamComparison = scrolled.ScrolledPanel(self.Notebook, -1)

            self.MyTeam.SetupScrolling()
            self.TeamComparison.SetupScrolling()

            self.Notebook3 = wx.Notebook(self.Panel3, -1)
            self.Analysis = scrolled.ScrolledPanel(self.Notebook3, -1)

            self.Analysis.SetupScrolling()

            self.AnalysisUL = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
            self.AnalysisUR = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
            self.AnalysisLL = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)
            self.AnalysisLR = wx.Panel(self.Analysis,-1, style=wx.BORDER_NONE)

            # Top Left Box (To Fix!)
            self.Picks_Left = wx.ListCtrl(self.AnalysisUL,-1,style=wx.LC_REPORT | wx.BORDER_NONE)
            self.Picks_Left.InsertColumn(1,'                  ')
            self.Picks_Left.InsertColumn(2,'    ',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(3,'H1',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(4,'H2',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(5,'H#',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(6,'H4',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(7,'H5',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(8,'H6',format=wx.LIST_FORMAT_CENTRE)
            self.Picks_Left.InsertColumn(9,'H7',format=wx.LIST_FORMAT_CENTRE)

##            table_width = self.Picks_Left.GetSize()[0] #GetSize returns (width, height) tuple
##            print table_width
##            num_col = self.Picks_Left.GetColumnCount()
##            col_width = table_width/num_col
##            for i in range(0, num_col):
##                self.Picks_Left.SetColumnWidth(i, col_width)

            self.Picks_Left.SetColumnWidth(0,-2)
            self.Picks_Left.SetColumnWidth(1,-2)
            self.Picks_Left.SetColumnWidth(2,-2)
            self.Picks_Left.SetColumnWidth(3,-2)
            self.Picks_Left.SetColumnWidth(4,-2)
            self.Picks_Left.SetColumnWidth(5,-2)
            self.Picks_Left.SetColumnWidth(6,-2)
            self.Picks_Left.SetColumnWidth(7,-2)
            self.Picks_Left.SetColumnWidth(8,-2)            

            # Bottom Left Box (Suggested Optimal Teams)
            self.Notebook_AltTeams = wx.Notebook(self.AnalysisLL, -1)
            self.Alt_Team_1 = scrolled.ScrolledPanel(self.Notebook_AltTeams, -1)
            self.Alt_Team_2 = scrolled.ScrolledPanel(self.Notebook_AltTeams, -1)
            self.Alt_Team_3 = scrolled.ScrolledPanel(self.Notebook_AltTeams, -1)
            self.Alt_Team_4 = scrolled.ScrolledPanel(self.Notebook_AltTeams, -1)
            self.Alt_Team_5 = scrolled.ScrolledPanel(self.Notebook_AltTeams, -1)

            self.Alt_Team_1.SetupScrolling()
            self.Alt_Team_2.SetupScrolling()
            self.Alt_Team_3.SetupScrolling()
            self.Alt_Team_4.SetupScrolling()
            self.Alt_Team_5.SetupScrolling()

            # Menu Bar
            self.MainMenu = wx.MenuBar()
            self.FileMenu = wx.Menu()

            self.FileOpenItem = wx.MenuItem(self.FileMenu, 103, "&Open\tCtrl+O", "Open a Previous Session", wx.ITEM_NORMAL)
            self.FileMenu.AppendItem(self.FileOpenItem)
            self.FileReloadItem = wx.MenuItem(self.FileMenu, 104, "&Reload Defaults", "Reload Default Files", wx.ITEM_NORMAL)
            self.FileMenu.AppendItem(self.FileReloadItem)
            self.FileSaveItem = wx.MenuItem(self.FileMenu, 102, "&Save\tCtrl+S", "Save the data", wx.ITEM_NORMAL)
            self.FileMenu.AppendItem(self.FileSaveItem)
            self.FileQuitItem = wx.MenuItem(self.FileMenu, wx.ID_EXIT, "&Quit\tCtrl+Q", "Quit the program", wx.ITEM_NORMAL)
            self.FileMenu.AppendItem(self.FileQuitItem)          
            self.MainMenu.Append(self.FileMenu, "&File")

            self.SetMenuBar(self.MainMenu)
            # Menu Bar end

            self.StatusBar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)

            self.__set_properties()
            self.__do_layout()

            self.Bind(wx.EVT_MENU, self.OnFileQuit, self.FileQuitItem)
            self.Bind(wx.EVT_MENU, self.OnFileSave, self.FileSaveItem)
            self.Bind(wx.EVT_MENU, self.OnFileOpen, self.FileOpenItem)
            self.Bind(wx.EVT_MENU, self.OnFileReload, self.FileReloadItem)

            self.OnAnalyze()


    def __set_properties(self): #Set up GUI Title and Map Window

            self.SetTitle("Test")
            screen_x = 95 * wx.GetDisplaySize()[0]/100
            screen_y = 90 * wx.GetDisplaySize()[1]/100
            self.SetSize((screen_x, screen_y))

            self.SetFocus()

    def __do_layout(self , call_fit = True, set_sizer = True): #Create Lay-Out of GUI

            # Sizer for Panel 3 (Analysis Page)
            hbox  = wx.BoxSizer(wx.HORIZONTAL)

            vbox1 = wx.BoxSizer(wx.VERTICAL)
            vbox2 = wx.BoxSizer(wx.VERTICAL)

            hbox2 = wx.BoxSizer(wx.HORIZONTAL)

            vbox1.Add(self.AnalysisUL, 1, wx.EXPAND | wx.ALL, 3)
            vbox1.Add(self.AnalysisLL, 1, wx.EXPAND | wx.ALL, 3)
            vbox2.Add(self.AnalysisUR, 1, wx.EXPAND | wx.ALL, 3)
            vbox2.Add(self.AnalysisLR, 1, wx.EXPAND | wx.ALL, 3)

            # Analysis Button Panel
            vbox_UL = wx.BoxSizer(wx.VERTICAL)
            vbox_UL3 = wx.BoxSizer(wx.HORIZONTAL)

            fontUL = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)
            self.UL_text = wx.StaticText(self.AnalysisUL, -1, 'Title 1')
            self.UL_text.SetFont(fontUL)

            Exp_Pts = ' '

            fontUL = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)
            self.ULText1 = wx.StaticText(self.AnalysisUL, -1, 'Title 2')
            self.ULText2 = wx.StaticText(self.AnalysisUL, -1, Exp_Pts)

            self.ULText1.SetFont(fontUL)
            self.ULText2.SetFont(fontUL)
            self.ULText2.SetForegroundColour((0,0,255))
            self.ULText1.SetForegroundColour((0,0,255))

            hbox2.Add(self.ULText1, 0)
            hbox2.Add(self.ULText2, 0)

            vbox_UL.Add(self.UL_text, 0, wx.CENTER|wx.ALL,20)
            vbox_UL.Add(self.Picks_Left,1,wx.EXPAND|wx.ALL,3)
            vbox_UL.Add(hbox2, 0, wx.CENTER|wx.BOTTOM,50)
            self.AnalysisUL.SetSizer(vbox_UL)

            # Suggested Pick Panel
            vbox_LR = wx.BoxSizer(wx.HORIZONTAL)
            vbox_LR2 = wx.BoxSizer(wx.VERTICAL)

            font2 = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)

            SuggestedSelection = ' '

            self.SuggestedText1 = wx.StaticText(self.AnalysisLR, -1, ' ')
            self.SuggestedText2 = wx.StaticText(self.AnalysisLR, -1, SuggestedSelection)

            self.SuggestedText1.SetFont(font2)
            self.SuggestedText2.SetFont(font2)
            self.SuggestedText2.SetForegroundColour((0,0,255))

            vbox_LR2.Add(self.SuggestedText1,0,wx.ALIGN_CENTRE)

            vbox_LR2.Add(wx.StaticText(self.AnalysisLR, -1, ''),0,wx.CENTER)
            vbox_LR2.Add(wx.StaticText(self.AnalysisLR, -1, ''),0,wx.CENTER)

            vbox_LR2.Add(self.SuggestedText2,0,wx.ALIGN_CENTRE)
            vbox_LR.Add(vbox_LR2,1,wx.CENTER)
            self.AnalysisLR.SetSizer(vbox_LR)

            # Projected Team Panel
            sizer_LL = wx.BoxSizer(wx.VERTICAL)
            self.Alt_Team_1.SetSizer(sizer_LL)

            sizer_LL = wx.BoxSizer(wx.VERTICAL)
            self.Alt_Team_2.SetSizer(sizer_LL)

            sizer_LL = wx.BoxSizer(wx.VERTICAL)
            self.Alt_Team_3.SetSizer(sizer_LL)

            sizer_LL = wx.BoxSizer(wx.VERTICAL)
            self.Alt_Team_4.SetSizer(sizer_LL)

            sizer_LL = wx.BoxSizer(wx.VERTICAL)
            self.Alt_Team_5.SetSizer(sizer_LL)

            # Picks Remaining Panel
            vbox_UR = wx.BoxSizer(wx.VERTICAL)

            fontUR = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)
            self.UR_text = wx.StaticText(self.AnalysisUR, -1, 'Title')
            self.UR_text.SetFont(fontUR)

            vbox_UR.Add(self.UR_text, 0, wx.CENTER)
            self.AnalysisUR.SetSizer(vbox_UR)

            # Finish Analysis Sizers
            hbox.Add(vbox1, 1, wx.EXPAND)
            hbox.Add(vbox2, 1, wx.EXPAND)
            self.Analysis.SetSizer(hbox)

            # Add Notebook Pages
            self.Notebook.AddPage(self.MyTeam,"A")
            self.Notebook.AddPage(self.TeamComparison,"B")

            self.Notebook3.AddPage(self.Analysis,"CHECK ME")

            self.Notebook_AltTeams.AddPage(self.Alt_Team_1,"1")
            self.Notebook_AltTeams.AddPage(self.Alt_Team_2,"2")
            self.Notebook_AltTeams.AddPage(self.Alt_Team_3,"3")
            self.Notebook_AltTeams.AddPage(self.Alt_Team_4,"4")
            self.Notebook_AltTeams.AddPage(self.Alt_Team_5,"5")

            #Sizer for Panel 1
            sizer_P1 = wx.BoxSizer(wx.VERTICAL)
            sizer_P1.Add(self.Notebook, 1, wx.EXPAND, 0)
            self.Panel1.SetSizer(sizer_P1)

            #Sizer for Panel 3
            sizer_P3 = wx.BoxSizer(wx.VERTICAL)
            sizer_P3.Add(self.Notebook3, 1, wx.EXPAND, 0)
            self.Panel3.SetSizer(sizer_P3)

            #Sizer for Panel (Alt_Teams)
            fontLL = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD)
            self.LL_text = wx.StaticText(self.AnalysisLL, -1, 'Title')
            self.LL_text.SetFont(fontLL)

            vbox_LL = wx.BoxSizer(wx.VERTICAL)
            vbox_LL.Add(self.LL_text, 0, wx.CENTER)
            #vbox_LL.Add(self.Notebook_AltTeams,1,wx.EXPAND,0)
            self.AnalysisLL.SetSizer(vbox_LL)

            # Split Panel Sizer
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.Splitter1,1,wx.EXPAND)
            self.SetSizer(sizer)

            self.Layout()
            self.Centre()


    # Function closes GUI 
    def OnFileQuit(self,event):
        self.Close()

    # Function reloads default parameters/lists
    def OnFileReload(self,event):
        self.Close()


    # Function loads previously saved files
    def OnFileOpen(self,event):
        self.Close()

    # Function saves current work
    def OnFileSave(self,event):
        self.Close()

    # Function analyzes draft, points, adp to suggest an optimal team and the next selection
    def OnAnalyze(self):

        self.Picks_Left.DeleteAllItems()


        self.Picks_Left.InsertStringItem(0, ' ')
        self.Picks_Left.InsertStringItem(1, 'Line 1')
        self.Picks_Left.InsertStringItem(2, ' ')
        self.Picks_Left.InsertStringItem(3, 'Line 2')
        self.Picks_Left.InsertStringItem(4, ' ')
        self.Picks_Left.InsertStringItem(5, 'Line 3')

        self.Picks_Left.SetStringItem(1,2, '1')
        self.Picks_Left.SetStringItem(1,3, '2')
        self.Picks_Left.SetStringItem(1,4, '3')
        self.Picks_Left.SetStringItem(1,5, '4')
        self.Picks_Left.SetStringItem(1,6, '5')
        self.Picks_Left.SetStringItem(1,7, '6')
        self.Picks_Left.SetStringItem(1,8, '7')

        self.Picks_Left.SetStringItem(3,2, '1')     
        self.Picks_Left.SetStringItem(3,3, '1')
        self.Picks_Left.SetStringItem(3,4, '1')
        self.Picks_Left.SetStringItem(3,5, '1')
        self.Picks_Left.SetStringItem(3,6, '1')
        self.Picks_Left.SetStringItem(3,7, '1')
        self.Picks_Left.SetStringItem(3,8, '1')

        self.Picks_Left.SetStringItem(5,2, '2')        
        self.Picks_Left.SetStringItem(5,3, '2')   
        self.Picks_Left.SetStringItem(5,4, '2')   
        self.Picks_Left.SetStringItem(5,5, '2')   
        self.Picks_Left.SetStringItem(5,6, '2')   
        self.Picks_Left.SetStringItem(5,7, '2')   
        self.Picks_Left.SetStringItem(5,8, '2')   

        self.AnalysisUL.Layout()

app = wx.App(0)

# Code to Execute File
class TApplication(wx.App):
    def OnInit(self):

            wx.InitAllImageHandlers()
            MainForm = TMainForm(None, -1,"")
            self.SetTopWindow(MainForm)

            MainForm.Show()
            return 1

if __name__ == "__main__":
    Application = TApplication(0)
    Application.MainLoop()

2 个答案:

答案 0 :(得分:2)

我认为GetSize()方法的问题可能是你在sizer的效果发生之前调用了它,所以它在sizer调整大小之前返回了ListCtrl的原始大小以适应面板。在代码的最后一行之后尝试使用此代码

table_width = self.Picks_Left.GetSize()[0] #GetSize returns (width, height) tuple
num_col = self.Picks_Left.GetColumnCount()
col_width = table_width/num_col
for i in range(0, num_col):
    self.Picks_Left.SetColumnWidth(i, col_width)

答案 1 :(得分:1)

您应该使用RowColSizer,这非常适合您的请求。这是一个小型演示如何使用它:

import  wx  
import  wx.lib.rcsizer  as rcs

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        sizer = rcs.RowColSizer()

        text = "This sizer lays out it's items by row and column "\
               "that are specified explicitly when the item is \n"\
               "added to the sizer.  Grid cells with nothing in "\
               "them are supported and column- or row-spanning is \n"\
               "handled as well.  Growable rows and columns are "\
               "specified just like the wxFlexGridSizer."

        sizer.Add(wx.StaticText(self, -1, text), row=1, col=1, colspan=5)

        sizer.Add(wx.TextCtrl(self, -1, "(3,1)"), flag=wx.EXPAND, row=3, col=1)
        sizer.Add(wx.TextCtrl(self, -1, "(3,2)"), row=3, col=2)
        sizer.Add(wx.TextCtrl(self, -1, "(3,3)"), row=3, col=3)
        sizer.Add(wx.TextCtrl(self, -1, "(3,4)"), row=3, col=4)
        sizer.Add(
            wx.TextCtrl(self, -1, "(4,2) span:(2,2)"), 
            flag=wx.EXPAND, row=4, col=2, rowspan=2, colspan=2
            )

        sizer.Add(wx.TextCtrl(self, -1, "(6,4)"), row=6, col=4)
        sizer.Add(wx.TextCtrl(self, -1, "(7,2)"), row=7, col=2)
        sizer.Add(wx.TextCtrl(self, -1, "(8,3)"), row=8, col=3)
        sizer.Add(
            wx.TextCtrl(self, -1, "(10,1) colspan: 4"), 
            flag=wx.EXPAND, pos=(10,1), colspan=4
            )

        sizer.Add(
            wx.TextCtrl(self, -1, "(3,5) rowspan: 8, growable col", style=wx.TE_MULTILINE),
            flag=wx.EXPAND, pos=(3,5), size=(8,1)
            )

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(wx.Button(self, -1, "A vertical box"), flag=wx.EXPAND)
        box.Add(wx.Button(self, -1, "sizer put in the"), flag=wx.EXPAND)
        box.Add(wx.Button(self, -1, "RowColSizer at (12,1)"), flag=wx.EXPAND)
        sizer.Add(box, pos=(12,1))

        sizer.Add(
            wx.TextCtrl(self, -1, "(12,2) align bottom"), 
            flag=wx.ALIGN_BOTTOM, pos=(12,2)
            )

        sizer.Add(
            wx.TextCtrl(self, -1, "(12,3) align center"), 
            flag=wx.ALIGN_CENTER_VERTICAL, pos=(12,3)
            )

        sizer.Add(wx.TextCtrl(self, -1, "(12,4)"),pos=(12,4))
        sizer.Add(
            wx.TextCtrl(self, -1, "(12,5) full border"), 
            flag=wx.EXPAND|wx.ALL, border=15, pos=(12,5)
            )

        sizer.AddGrowableCol(5)
        sizer.AddGrowableRow(9)

        sizer.AddSpacer(10,10, pos=(1,6))
        sizer.AddSpacer(10,10, pos=(13,1))

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


app = wx.App()
frame = wx.Frame(None, -1)
pn = TestPanel(frame)
frame.Show()
frame.Maximize()
app.MainLoop()

使用aui

可以通过更奇特的方式实现相同的请求

希望它可以帮到你。