I'm doing an app in PyQt through Qt Designer and I've populated a (container widget inside a) Scroll Area with a list of cards (custom widgets that contains informations). I've put outside of the scroll area a QLineEdit and I want to use this QLineEdit to filter the cards based on specific attributes of each card (name, id, username). Is there any way to do this?
I know the question is a bit poorly written, but I'm a bit lost on how should I approach this problem. I tried to search for "searchbar" and "filter", but nothing looks like what I need.
Here's a sample of my current code (without any attempt of implementing the search function):
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# users_df: dataframe with users data
users_df, groups_df = extract_load_data()
# scrollArea
scroll_area = self.ui.scrollArea_cards_members
# we create a widget container
content_widget = QWidget()
# we set this container as the widget of the scroll area
scroll_area.setWidget(content_widget)
scroll_area.setWidgetResizable(True)
# set layout
self.scroll_layout = QVBoxLayout(content_widget)
# iterate in the dataframe
for idx in users_df.index:
member_series = users_df.iloc[idx]
self.member_card = MemberCard(member_series)
self.scroll_layout.addWidget(self.member_card)
# make the scroll area justified top
self.scroll_layout.addStretch()
self.show()
And the MemberCard looks like:
class MemberCard(QWidget):
'''
Member card widget class
'''
def __init__(self, member_series, parent=None):
'''
Parameters
----------
card_container:
member_series: pd.Series
series formed by the integer location of the ```users_df```
(member_series = users_df.iloc[x])
'''
super(MemberCard, self).__init__(parent)
self.dict = dict(member_series)
# Ui_MemberCard: class created by QtDesigner
self.card = Ui_MemberCard()
self.card.setupUi(self)
self.fill_card_info()
def fill_card_info(self,):
'''
Method that fills the informations of the Member Card
'''
self.card.name_label.setText(self.dict['name'])
self.card.username.setText(self.dict['username'])
self.card.id.setText(str(self.dict['id']))
self.card.joined_in.setText(self.dict['created_at'])
What #jfaccioni commented in the original post was really a clear, easy, and effective solution, so I will make this question as answered posting it here. To connect these fields you need to create one method to update the scrollArea and one function to verify the matches. For me it was something like this:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# users_df: dataframe with users data
users_df, groups_df = extract_load_data()
# scrollArea
scroll_area = self.ui.scrollArea_cards_members
# we create a widget container
content_widget = QWidget()
# we set this container as the widget of the scroll area
scroll_area.setWidget(content_widget)
scroll_area.setWidgetResizable(True)
# set layout
self.scroll_layout = QVBoxLayout(content_widget)
# iterate in the dataframe
for idx in users_df.index:
member_series = users_df.iloc[idx]
self.member_card = MemberCard(member_series)
self.scroll_layout.addWidget(self.member_card)
# make the scroll area justified top
self.scroll_layout.addStretch()
self.show()
def update_members(self, string):
'''
Method that hides/shows member cards based on a search string
Parameters
----------
string: str
string input on the searchbar
'''
for member_card in self.member_cards:
visible = filter_members(string, member_card)
member_card.setVisible(visible)
And the auxiliary filter_members function:
def filter_members(string, member_card):
'''
Filter function that can filter a member_card based on a string
The member_card will be filtered by name, username, or id
Parameters
----------
string: str
string input in the QLineEdit searchbar
member_card: :obj: MemberCard
MemberCard object to be tested against the string
Returns
-------
bool
'''
member_id = str(member_card.dict['id'])
member_name = member_card.dict['name'].lower()
member_username = member_card.dict['username'].lower()
string = string.lower()
return ((string in member_id) or (string in member_name) or (string in member_username))
I am writing a tool that allows me to track some tasks along a path of predifined stages, from something on a backlog, to ToDo, through WIP, Review and finally to done.
I created a custom widget, that will eventually be yellow, not unlike a postit note and perhaps with a bit of formatting it to give it a nice frame, etc... but stopped before getting far enough to make it look right because of this issue.
The idea is that each of these yellow Task widgets will have a stage they are at, and that I can select them in a Table Widget, and move them onto the next or previous stage, which will update taht objects stage, then refresh the TableWidget, read all the widget and where thay should be and set them in their new place.
So I have it kind of working to some degree (below), where I can move the tasks forward and they update location, but I noticed when I click the cells that the widget was previously in, print statement still says that the cell still has a widget there (which kind of makes sense, as code below isn't removing the previous one, but I'd expect to visually still see it). And I can move them forward and backwards, and the information on the tasks does update correctly, but the table won't refresh unless the task moves to a cell that never had a cellWidget in it. Test this by moving it backwards. It works, movnig forward visually does nothing, but moving again, does show up.
I tried clearing the TableWidget and rebuilding from scratch and that crashes. The main issue I am having is that with all these crashes, which is an issue in itself as it makes debugging very tough... When I try and clear the TableWidget (with .clear()) before repopulating, I get this.
Process finished with exit code -1073741819 (0xC0000005)
Same error code if I try removing the old cells by setting the Table Widget to 0 rows before adding the correct number of rows.
A known issue that is less important is when I select a cell without a widget and try and move it, gies me this, but don't worry too much about that fix, as it's known issue.
Process finished with exit code -1073740791 (0xC0000409)
Also tried cleaning up by iterating every cell and if it has a cell widget, remove cell widget before re-setting them to correct place and it still crashes. I'm out of ideas.
Task Widget
import sys
from PyQt5.QtWidgets import (QApplication, QTableWidget, QWidget, QFrame, QHBoxLayout, QLabel,
QPushButton,QVBoxLayout)
class Task(QWidget):
def __init__(self, ID, name, est):
super(Task, self).__init__()
# Creates a small widget that will be added to a table widget
self.ID = ID
self.name = name
self.est = est
# These cell widgets represent tasks. So each task has a particular 'stage' it is at
self.stage = 'ToDo'
self.stages = ['Backlog', 'ToDo', 'WIP', 'Review', 'Done']
self.objects_labels = {}
self.initUI()
def initUI(self):
# adds a bunch of labels to the widget
layout = QVBoxLayout()
frame = QFrame()
frame.setFrameShape(QFrame.StyledPanel)
frame.setStyleSheet('background-color: red')
frame.setLineWidth(2)
layout.addWidget(frame)
info = [self.ID, self.name, self.est]
for section in info:
self.objects_labels[section] = QLabel(str(section))
layout.addWidget(self.objects_labels[section])
self.setLayout(layout)
self.setStyleSheet('background-color: yellow')
def task_move(self, forward = True):
# The main widget will allow me to change the stage of a particular Task
# The idea is that I update the Table widget to show everything in the right place
# This function finds out what stage it is at and increments/decrements by one
index = self.stages.index(self.stage)
print(self.stages)
print(index)
if forward:
print('--->')
if self.stage == self.stages[-1]:
print('Already at the end of process')
return
self.stage = self.stages[index + 1]
else:
print('<---')
if self.stage == self.stages[0]:
print('Already at the start of process')
return
self.stage = self.stages[index - 1]
MainWidget
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.tasks = self.make_tasks()
self.init_ui()
self.update_tw()
def make_tasks(self):
# Create a few tasks
a = Task(0, 'Name_A', 44)
b = Task(0, 'Name_B', 22)
c = Task(0, 'Name_C', 66)
d = Task(0, 'Name_D', 90)
return [a, b, c, d]
def init_ui(self):
layout_main = QVBoxLayout()
self.tw = QTableWidget()
self.tw.cellClicked.connect(self.cell_clicked)
self.tw.horizontalHeader().setDefaultSectionSize(120)
self.tw.verticalHeader().setDefaultSectionSize(120)
layout_main.addWidget(self.tw)
layout_bottom_button_bar = QHBoxLayout()
self.btn_task_backward = QPushButton('<--- Task')
self.btn_task_backward.clicked.connect(lambda: self.move_task(forward=False))
self.btn_task_forward = QPushButton('Task --->')
self.btn_task_forward.clicked.connect(lambda: self.move_task())
for widget in [self.btn_task_backward, self.btn_task_forward]:
layout_bottom_button_bar.addWidget(widget)
layout_main.addLayout(layout_bottom_button_bar)
self.setLayout(layout_main)
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle('MainWidget')
self.show()
#property
def tw_header(self):
return {'Backlog': 0, 'ToDo': 1, 'WIP': 2, 'Review': 3, 'Done': 4}
#property
def selected_indices(self):
return [(x.row(), x.column()) for x in self.tw.selectedIndexes()]
#property
def selected_widgets(self):
selected_widgets = [self.tw.cellWidget(x[0], x[1]) for x in self.selected_indices]
print(selected_widgets)
return selected_widgets
def move_task(self, forward=True):
# Crashes if you select a non-widget cell, but thats a known issue
# Moves the task forward or backward and then prompts to update the TableWidget
for object in self.selected_widgets:
object.task_move(forward=forward)
self.tw.clearSelection()
self.update_tw()
def cell_clicked(self, row, column):
if self.tw.cellWidget(row, column):
print(self.selected_indices)
print(self.selected_widgets)
else:
print('No Cell Widget here')
def update_tw(self):
#I wanted to clear the Table widget and rebuild, but this crashes
# self.tw.clear()
self.tw.setHorizontalHeaderLabels(self.tw_header.keys())
rows = len(self.tasks)
columns = len(self.tw_header)
self.tw.setRowCount(rows)
self.tw.setColumnCount(columns)
# Looks through each task, and then gets it's stage, and then adds the widget to the correct column
for index, object in enumerate(self.tasks):
column = self.tw_header[object.stage]
print('Setting stage {} for {}\n...to r={}, c={}\n***'.format(object.stage, object, index, column))
self.tw.setCellWidget(index, column, object)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())
From my previous experience, I always found using setCellWidget clunky, underperforming and buggy.
Most of the times my Widgets were lost or misplaced, while refreshing the table similarly to the way you are doing it.
In addition, I guess you would want to use this "Task Mover" on a larger scale, and from what I could see, setting separate Widgets inside QWidgetItems becomes quite slow when done on loads of items.
My suggestion would be to use style delegates, so that you can customize the look of your items to your liking, without having to deal with the setCellWidget stuff which is giving you problem.
Once you have your own delegate, and paint the items the way you want, you can just keep updating that item data and moving the items around the table by using "take" and "set".
I am not sure if this would be the best way of executing this specific task, but moving towards this direction would probably give you greater flexibility and customisation power in the long run.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class TaskProperty():
properties = ["ID", "name", "est", "stage"]
count = 4
ID, Name, Est, Stage = [Qt.UserRole + x for x in range(count)]
STAGES = ['Backlog', 'ToDo', 'WIP', 'Review', 'Done']
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.tasks = self.make_tasks()
self.init_ui()
self.update_tw()
def make_tasks(self):
# Create a few tasks
a = Task(0, 'Name_A', 44)
b = Task(0, 'Name_B', 22)
c = Task(0, 'Name_C', 66)
d = Task(0, 'Name_D', 90)
return [a, b, c, d]
def init_ui(self):
layout_main = QVBoxLayout()
self.tw = QTableWidget()
# create and set the delegate to the TableWidget
self.delegate = TaskDelegate(self.tw )
self.tw.setItemDelegate(self.delegate)
self.tw.cellClicked.connect(self.cell_clicked)
self.tw.horizontalHeader().setDefaultSectionSize(120)
self.tw.verticalHeader().setDefaultSectionSize(120)
layout_main.addWidget(self.tw)
layout_bottom_button_bar = QHBoxLayout()
self.btn_task_backward = QPushButton('<--- Task')
self.btn_task_backward.clicked.connect(lambda: self.move_task(forward=False))
self.btn_task_forward = QPushButton('Task --->')
self.btn_task_forward.clicked.connect(lambda: self.move_task())
for widget in [self.btn_task_backward, self.btn_task_forward]:
layout_bottom_button_bar.addWidget(widget)
layout_main.addLayout(layout_bottom_button_bar)
self.setLayout(layout_main)
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle('MainWidget')
self.show()
#property
def tw_header(self):
return {'Backlog': 0, 'ToDo': 1, 'WIP': 2, 'Review': 3, 'Done': 4}
#property
def selected_indices(self):
return [(x.row(), x.column()) for x in self.tw.selectedIndexes()]
def move_task(self, forward=True):
'''
To move the task to the next step, we iterate all the items selected.
If the task can be moved, we take the corresponding item from its current cell and move it to the destination.
:param forward:
:return:
'''
selected =self.tw.selectedItems()
for item in selected:
item.setSelected(False)
result = item.task_move(forward=forward)
if result:
next = 1 if forward else -1
row = item.row()
column = item.column()
moveItem = self.tw.takeItem(row, column)
self.tw.setItem(row, column + next, moveItem)
moveItem.setSelected(True)
def cell_clicked(self, row, column):
item = self.tw.item(row, column)
if not isinstance(item, TaskItem):
print "No Task Item Here"
def update_tw(self):
# I wanted to clear the Table widget and rebuild, but this crashes
# self.tw.clear()
self.tw.clear()
self.tw.setHorizontalHeaderLabels(self.tw_header.keys())
rows = len(self.tasks)
columns = len(self.tw_header)
self.tw.setRowCount(rows)
self.tw.setColumnCount(columns)
# Looks through each task, and then gets it's stage, and then adds the widget to the correct column
for row, object in enumerate(self.tasks):
# create items of our custom type only for the column that need to be filled.
# the other cells will be filled with null items.
column = STAGES.index(object.stage)
print('Setting stage {} for {}\n...to r={}, c={}\n***'.format(object.stage, object, row, column))
item = TaskItem(object)
self.tw.setItem(row, column, item)
class TaskDelegate(QStyledItemDelegate):
'''
This delegate take care of Drawing our cells the way we want it to be.
'''
def paint(self, painter, option, index):
'''
Override the Paint function to draw our own cell.
If the QTableWidgetItem does not have our Data stored in it, we do a default paint
:param painter:
:param option:
:param index:
:return:
'''
painter.save()
rect = option.rect
status = index.data(TaskProperty.Stage)
if status is None:
return super(TaskDelegate, self).paint(painter, option, index)
else:
id = STAGES.index(status)
pen = painter.pen()
pen.setBrush(Qt.black)
painter.setPen(pen)
if id == index.column():
rect.translate(3, 3)
newRect = QRect(rect.x(), rect.y(), rect.width() - 6, 20)
infos = [index.data(TaskProperty.ID), index.data(TaskProperty.Name), index.data(TaskProperty.Est)]
painter.setBrush(Qt.red)
painter.drawRect(newRect)
painter.setBrush(Qt.yellow)
for info in infos:
newRect.translate(0, 25)
painter.drawRect(newRect)
painter.drawText(newRect, Qt.AlignHCenter | Qt.AlignVCenter,
str(info))
class TaskItem(QTableWidgetItem):
'''
Subclass QTableWidgetItem.
Probably not needed, since we can set the property when we create the item instead of in the init,
and keep track of which item is attached to which task object using the Column Index of the table.
However, this can be useful if you want to attach more specific procedures to your items
'''
def __init__(self, task):
super(TaskItem, self).__init__()
self._task = task
self.setData(TaskProperty.ID, task.ID)
self.setData(TaskProperty.Name, task.name)
self.setData(TaskProperty.Est, task.est)
self.setData(TaskProperty.Stage, task.stage)
self.objects_labels = {}
def task_move(self, forward=True):
result = self._task.task_move(forward=forward)
self.setData(TaskProperty.Stage, self._task.stage)
return result
class Task(object):
'''
The Task class is now just an object, not a widget.
'''
def __init__(self, ID, name, est):
# Creates a small widget that will be added to a table widget
self.ID = ID
self.name = name
self.est = est
# These cell widgets represent tasks. So each task has a particular 'stage' it is at
self.stage = 'ToDo'
self.stages = ['Backlog', 'ToDo', 'WIP', 'Review', 'Done']
self.objects_labels = {}
def task_move(self, forward=True):
# The main widget will allow me to change the stage of a particular Task
# The idea is that I update the Table widget to show everything in the right place
# This function finds out what stage it is at and increments/decrements by one
index = self.stages.index(self.stage)
if forward:
print('--->')
if self.stage == self.stages[-1]:
#print('Already at the end of process')
return False
self.stage = self.stages[index + 1]
else:
print('<---')
if self.stage == self.stages[0]:
#print('Already at the start of process')
return False
self.stage = self.stages[index - 1]
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())
It is not necessary to clean and create everything again, instead just move the widget for it we must know if it can be moved or not and for that task_move must indicate if the movement is valid or not. Considering the above, the solution is:
def task_move(self, forward=True):
# The main widget will allow me to change the stage of a particular Task
# The idea is that I update the Table widget to show everything in the right place
# This function finds out what stage it is at and increments/decrements by one
index = self.stages.index(self.stage)
print(self.stages)
print(index)
if forward:
print("--->")
if self.stage == self.stages[-1]:
print("Already at the end of process")
return False
self.stage = self.stages[index + 1]
else:
print("<---")
if self.stage == self.stages[0]:
print("Already at the start of process")
return False
self.stage = self.stages[index - 1]
return True
def move_task(self, forward=True):
for row, column in self.selected_indices:
widget = self.tw.cellWidget(row, column)
if isinstance(widget, Task) and widget.task_move(forward):
next_column = column + (1 if forward else -1)
# create new task widget
task = Task(widget.ID, widget.name, widget.est)
# remove all task widget
self.tw.removeCellWidget(row, column)
# move task widget
self.tw.setCellWidget(row, next_column, task)
self.tw.clearSelection()
The crashed is because when using clear you are also removing the Task widget so "self.tasks" has objects deleted from C++ that you should not use.
I am writing an app that dynamically adds and removes widgets to a QScrollView. The code below, using Qt3 and python, will give me dynamic widgets, but when I add too many to be seen, no scroll bar appears. It is not yet scrollable. I've put the relevant pieces of code below.
Any answers must be in Qt3 because my company only uses Qt3. I'm new to programming and Qt in general.
PL = parser.Plist()
class EC_Conf_App(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.gridLayout = QGridLayout(self)
self.scrollArea = QScrollView(self)
self.scrollArea.setGeometry(0, 0, 369, 286)
self.Form1Layout = QGridLayout(self.scrollArea)
self.gridLayout.addWidget(self.scrollArea, 0, 0)
for item in PL.plist:
self.section_create(item.name, item.variables)
def section_create(self, name, variables):
# ADD ROW BUTTON
for key, value in sorted(variables.iteritems()):
if len(value) > 3: # if there is more than one option for the combobox
self.addButton = QPushButton(self.scrollArea, name + '_AddButton')
self.addButton.setText('Add Row')
self.Form1Layout.addWidget(self.addButton, self.Ay, self.Ax)
self.addButton.show()
self.connect(self.addButton,SIGNAL("clicked()"),self.add_rows)
def add_rows(self):
self.addButton = self.sender()
self.addButton.name()
copy_class = self.addButton.name()
clean_name = copy_class[:-10]
for item in PL.plist:
if item.name == clean_name:
PL.insert(item.name, item.heading, item.variables)
self.remove_widgets()
break
def remove_widgets(self):
for item in self.widgets:
item.deleteLater()
self.Form1Layout.remove(item)
self.construct()
def construct(self):
for item in PL.plist:
self.section_create(item.name, item.variables)
The only way to use a layout with a QScrollView is to set the layout on its viewport(), not the view itself. This is documented.
Replace self.Form1Layout = QGridLayout(self.scrollArea) with
self.Form1Layout = QGridLayout(self.scrollArea.viewport())
This question deals with the same problem for Qt4/5.
I am creating a small app in python using PySide. I read lines from a text file and display each line in a separate QLineEdit Widget. Each "entry" has 2 line edits and 2 QPushButtons. For every line I add those widgets. My problem is that I set a signal-slot for the QPushButtons, but when all the "entries" are generated, only the last entries QPushButtons connects to the slot. May someone please help me.
Here is my code
class ItemLogger(QtGui.QMainWindow, Ui.Ui_MainWindow):
def __init__(self, parent = None):
super(ItemLogger, self).__init__(parent)
self.setupUi(self)
self.parseBossItem()
self.comboBox.currentIndexChanged.connect(self.parseBossItem)
self.increase.clicked.connect(self.add_subtract)
def add_subtract(self):
initial = 1
print "kajskasdjflsdkjflk"
def addRow(self, item):
self.frame = QtGui.QFrame()
self.layout = QtGui.QHBoxLayout()
self.itemName = QtGui.QLineEdit(item)
self.itemName.setReadOnly(True)
self.itemCount = QtGui.QLineEdit()
self.itemCount.setText("0")
self.itemCount.setMaximumWidth(40)
self.decrease = QtGui.QPushButton("-")
self.increase = QtGui.QPushButton("+")
self.layout.addWidget(self.itemName)
self.layout.addWidget(self.itemCount)
self.layout.addWidget(self.decrease)
self.layout.addWidget(self.increase)
self.frame.setLayout(self.layout)
self.verticalLayout_3.addWidget(self.frame)
def parseBossItem(self):
if self.comboBox.currentText() == "Item_1":
item_list = open("BossItems/Random_Item")
for line in item_list.readlines():
self.addRow(line)
if self.comboBox.currentText() == "Item_2":
item_list = open("BossItems/Random_Item_2")
for line in item_list.readlines():
self.addRow(line)
This is because you only connected the last entry.
Here is what you are actually doing:
You add row for item 1, and assign button widgets to self.decrease, self.increase.
You add row for item 2, replacing values of self.decrease, self.increase by newly created widgets.
You connect self.increase, which is now the last added widget.
If you don't need access to you widgets after their creation, you should consider using local variables (e.g. without self) and connecting the signal inside the addRow function.
If you need to keep track of widget references, then you could add them to an array:
# Somewhere in __init__ or in parseBossItem
self.increase = []
# in addRow
self.increase.append(QtGui.QPushButton("+"))
self.layout.addWidget(self.increase[-1])
self.increase[-1].clicked.connect(self.add_subtract)
# and so on...
To use the same slot form different senders, you need to identify who sent the signal. You could do something like this:
def onIncrease(self):
button = self.sender()
if isinstance(button, QtGui.QPushButton):
buttonName = button.text()
if buttonName == 'name of button 1':
self.itemCount[0].setText(str(int(self.itemCount[0])+1))
elif buttonName == 'name of button 2':
...
Off course, this is assuming you put each QLineEdit in the array self.itemCount.
Since all your buttons have the same name, we need to use another approach.
# in addRow
self.increase.clicked.connect(lambda: self.onIncrease(itemCount))
def onIncrease(self, edit):
edit.setText(str(int(edit.text()+1))
I wrote a simple program to print out all non-hidden files and subdirectories in a given directory.
I am now trying to migrate my code into a clist widget example I found on Google. Other than ripping out some unneeded buttons, all I changed was the top portion to integrate my code, and it partially works except that it only returns the first character of each file and subdirectory. So I expected this:
Desktop
Downloads
Scripts
textfile.txt
pron.avi
But instead got this:
D
D
S
t
p
Here is the example with the code I changed (really just the first def)
import gtk, os
class CListExample:
# this is the part Thraspic changed (other than safe deletions)
# User clicked the "Add List" button.
def button_add_clicked(self, data):
dirList=os.listdir("/usr/bin")
for item in dirList:
if item[0] != '.':
data.append(item)
data.sort()
return
def __init__(self):
self.flag = 0
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(250,150)
window.set_title("GtkCList Example")
window.connect("destroy", gtk.mainquit)
vbox = gtk.VBox(gtk.FALSE, 5)
vbox.set_border_width(0)
window.add(vbox)
vbox.show()
scrolled_window = gtk.ScrolledWindow()
scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
vbox.pack_start(scrolled_window, gtk.TRUE, gtk.TRUE, 0)
scrolled_window.show()
clist = gtk.CList(1)
# What however is important, is that we set the column widths as
# they will never be right otherwise. Note that the columns are
# numbered from 0 and up (to an anynumber of columns).
clist.set_column_width(0, 150)
# Add the CList widget to the vertical box and show it.
scrolled_window.add(clist)
clist.show()
hbox = gtk.HBox(gtk.FALSE, 0)
vbox.pack_start(hbox, gtk.FALSE, gtk.TRUE, 0)
hbox.show()
button_add = gtk.Button("Add List")
hbox.pack_start(button_add, gtk.TRUE, gtk.TRUE, 0)
# Connect our callbacks to the three buttons
button_add.connect_object("clicked", self.button_add_clicked,
clist)
button_add.show()
# The interface is completely set up so we show the window and
# enter the gtk_main loop.
window.show()
def main():
gtk.mainloop()
return 0
if __name__ == "__main__":
CListExample()
main()
When you adding data to CList through append method, you must pass a sequence. Rewrite your code:
def button_add_clicked(self, data):
dirList = os.listdir("/usr/bin")
for item in dirList:
if not item.startswith('.'):
data.append([item])
data.sort()
When you creating CList instance you passes to the constructor number of collumns. In your example you created CList with one collumn, that's why you can see only first element (first character) of passed sequence in the append method.