简体   繁体   中英

wxListCtrl not displaying properly

Help!

I am a non-GUI programmer who is trying to write a simple (!) program using wxPython.

I have read everything I can online, but my overall lack of GUI experience is presumably causing me to not see the problem.

In a nutshell, I want to have a window with a wxNotebook with several tabs. Each tab, of course, will have its own child widgets. I envision having either a wxListCtrl (as in my code) or possibly a wxGrid control, along with several buttons.

Here is my "EmployeesPanel" class. When I run this, I see a tiny square that must represent the listctrl, but for the life of me I cannot figure out how to make it look correct. Of course, it is possible that I am way off base in some other area(s) as well.

Any help as to what I am doing wrong would be greatly appreciated.

Here is the code:

import wx
import sys

employees = [('Earl Boffo', 'Software'), ('Mildred Plotka', 'Software'), ('Sugar Kane', 'QA')]

classes = [('Python'), ('Java'), ('C#')]

class EmployeesPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # create some sizers 
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        grid = wx.GridBagSizer(hgap=5, vgap=5)

        #hSizer = wx.BoxSizer(wx.HORIZONTAL|wx.EXPAND)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)

        panel = wx.Panel(self, -1)

        self.list = wx.ListCtrl(panel, size=(100,100), style=wx.LC_REPORT)

        self.list.InsertColumn(0, 'Name')
        self.list.InsertColumn(1, 'Group')

        for i in employees:
            index = self.list.InsertStringItem(sys.maxint, i[0])
            self.list.SetStringItem(index, 1, i[1])

        # A button
        self.button = wx.Button(self, label="Exit")
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
        self.list.Show(True)

        # add the listctrl widget to the grid
        grid.Add(self.list, pos=(0,0), flag=wx.EXPAND|wx.ALL)

        # add the button to the grid
        grid.Add(self.button, pos=(1,0))

        # add a spacer to the sizer
        grid.Add((10, 40), pos=(1,1))

        # add grid to hSizer
        hSizer.Add(grid, 0, wx.ALL, 5)

        # add hSizer to main (v) sizer
        mainSizer.Add(hSizer, 0, wx.ALL, 5)
        self.SetSizerAndFit(mainSizer)
        self.Show()

    def EvtComboBox(self, event):
        self.logger.AppendText('EvtComboBox: %s\n' % event.GetString())
    def OnClick(self,event):
        sys.exit(3)

app = wx.App(False)
frame = wx.Frame(None, title="Training Tracker", size=(700,500))
nb = wx.Notebook(frame)

nb.AddPage(EmployeesPanel(nb), "Employees")
frame.Show()
app.MainLoop()

Welcome to wxPython! It's actually a lot of fun once you get the hang of it. I almost never use the grid sizers as they're just a pain for simple layouts like this. If you have a grid like interface and you don't have controls that are going to stretch across cells, then it's great. Otherwise, I almost always use BoxSizers nested in each other. I simplified your code quite a bit to show the two widgets. Currently the list control only stretches horizontally. If you need it to go vertically too, then change the proportion from 0 to 1 in the sizer.Add part.

import wx
import sys

employees = [('Earl Boffo', 'Software'), ('Mildred Plotka', 'Software'), ('Sugar Kane', 'QA')]

classes = [('Python'), ('Java'), ('C#')]

class EmployeesPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        # create some sizers 
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.list = wx.ListCtrl(self, size=(100,100), style=wx.LC_REPORT)

        self.list.InsertColumn(0, 'Name')
        self.list.InsertColumn(1, 'Group')

        for i in employees:
            index = self.list.InsertStringItem(sys.maxint, i[0])
            self.list.SetStringItem(index, 1, i[1])

        # A button
        self.button = wx.Button(self, label="Exit")
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)

        mainSizer.Add(self.list, 0, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(self.button, 0, wx.ALL, 5)

        self.SetSizer(mainSizer)
        self.Show()

    def EvtComboBox(self, event):
        self.logger.AppendText('EvtComboBox: %s\n' % event.GetString())
    def OnClick(self,event):
        sys.exit(3)

app = wx.App(False)
frame = wx.Frame(None, title="Training Tracker", size=(700,500))
nb = wx.Notebook(frame)

nb.AddPage(EmployeesPanel(nb), "Employees")
frame.Show()
app.MainLoop()

I also think these articles might help you:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM