1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| import sys
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.init_ui()
def init_ui(self):
openAction = QtGui.QAction(QtGui.QIcon('exit.png'), "&Open file", self)
openAction.setShortcut('Ctrl+O')
openAction.triggered.connect(self.openFile)
exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), "&Quit", self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(self.close)
menubar = self.menuBar()
file_menu = menubar.addMenu("&File")
file_menu.addAction(openAction)
file_menu.addAction(exitAction)
self.list_view = ListView(self)
self.setCentralWidget(self.list_view)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("My title")
self.show()
def openFile(self):
file = OpenFileDialog(self)
class ListView(QtGui.QListView):
def __init__(self, parent):
super(ListView, self).__init__(parent)
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.layout = QtGui.QVBoxLayout(self)
self.liste_model = ListModel(self)
self.setModel(self.liste_model)
class ListModel(QtGui.QStandardItemModel):
def __init__(self, parent):
super(ListModel, self).__init__(parent)
foods = [
'Cookie dough', # Must be store-bought
'Hummus', # Must be homemade
'Spaghetti', # Must be saucy
'Dal makhani', # Must be spicy
'Chocolate whipped cream' # Must be plentiful
]
for food in foods:
item = QtGui.QStandardItem(food)
item.setCheckable(True)
# Checked by default
item.setCheckState(QtCore.Qt.Checked)
self.appendRow(item)
class OpenFileDialog(QtGui.QFileDialog):
def __init__(self, parent):
super(OpenFileDialog, self).__init__(parent)
fname, _ = self.getOpenFileName(self, 'Open file')
f = open(fname, 'r')
with f:
data = f.read()
print(data)
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main() |
Partager