One of my standard test programs for any GUI toolkit, creating, loading a listbox and selecting an item. Here is the PyQT code ...
# a simple window using PyQT
# with a button and a listbox to load and select
import sys
# might be easier to use this import option
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyForm(QWidget):
def __init__(self, name_list):
QWidget.__init__(self)
# setGeometry(x_pos, y_pos, width, height)
self.setGeometry(100, 150, 300, 220)
self.setWindowTitle("Load the listbox first")
# make name_list available for methods
self.name_list = name_list
# use a grid layout for the widgets
grid = QGridLayout()
btn_load = QPushButton("Load List")
# bind the button click to a function reference
self.connect(btn_load, SIGNAL("clicked()"), self.on_click)
self.listbox = QListWidget()
self.connect(self.listbox, SIGNAL("itemSelectionChanged()"), self.on_select)
# addWidget(widget, row, column, rowSpan, columnSpan)
grid.addWidget(btn_load, 0, 0, 1, 1)
# listbox spans over 5 rows and 2 columns
grid.addWidget(self.listbox, 1, 0, 5, 2)
self.setLayout(grid)
def on_click(self):
"""the load button has been clicked, load the listbox"""
self.listbox.addItems(self.name_list)
def on_select(self):
"""an item in the listbox has been clicked/selected"""
selected_name = self.listbox.selectedItems()[0].text()
self.setWindowTitle(selected_name)
name_list = [
"Erich Meitinger",
"Udo Baus",
"Jens Klein",
"Bjorn Bork",
"Heidrun Lovelace",
"Klaus Abraham",
"Ulla Jorgens",
"Volger Jenkings",
"Helmut Schmidt",
"Freja Larse",
"Larry Orkan",
"Andreas Mauser",
"Harry Heimlich"
]
app = QApplication(sys.argv)
form = MyForm(name_list)
form.show()
app.exec_()
I do miss the colors. Here is the corresponding wxPython code with some colors added ...
# a simple window using wxPython
# with a button and a listbox to load and select
import wx
class MyFrame(wx.Frame):
def __init__(self, name_list):
wx.Frame.__init__(self, parent=None)
self.SetBackgroundColour("green") # ah, color!
self.SetTitle('Load the listbox first')
# make name_list available for methods
self.name_list = name_list
# use a grid layout for the widgets
grid = wx.GridBagSizer()
self.btn_load = wx.Button(self, -1, "Load List")
# bind the button click to a function reference
self.btn_load.Bind(wx.EVT_BUTTON, self.on_click)
self.listbox = wx.ListBox(self)
self.listbox.SetBackgroundColour("yellow")
self.listbox.Bind(wx.EVT_LISTBOX, self.on_select)
# Add(widget, pos=(row, column), span=(rowspan, columnspan))
grid.Add(self.btn_load, pos=(0, 0), flag=wx.ALL, border=5)
# add a 180 pixel wide spacer
grid.Add((180, 0), pos=(0, 1))
grid.Add(self.listbox, pos=(1,0), span=(10,2),
flag=wx.ALL|wx.EXPAND, border=5)
self.SetSizerAndFit(grid)
def on_click(self, event):
"""the load button has been clicked, load the listbox"""
self.listbox.Set(self.name_list)
def on_select(self, event):
"""an item in the listbox has been clicked/selected"""
selected_name = self.listbox.GetStringSelection()
self.SetTitle(selected_name)
name_list = [
"Erich Meitinger",
"Udo Baus",
"Jens Klein",
"Bjorn Bork",
"Heidrun Lovelace",
"Klaus Abraham",
"Ulla Jorgens",
"Volger Jenkings",
"Helmut Schmidt",
"Freja Larse",
"Larry Orkan",
"Andreas Mauser",
"Harry Heimlich"
]
app = wx.App(0)
MyFrame(name_list).Show()
app.MainLoop()
The wx.GridBagSizer() seems to be more complex.