Python Gui code required
Prof. Dr. Thomas Schultz Mohammad Khatami ([email protected] )
Shekoufeh Gorgi Zadeh ([email protected])
B-IT Bonn-Aachen International
Center for Information Technology November 16, 2017
Winter term 2017/18
Introduction to Computer Science for Life Scientists Assignment Sheet 06
If you have questions concerning the exercises, please subscribe to our mailing list via the lecture webpage and write to [email protected].
This exercise can be submitted in small groups of 2−3 students. Please submit each solution only once, but clearly indicate who contributed to it and remember that all team members have to be able to explain it.
For this assignment, we recommend using an IDE like Spyder for programming, and git for collaboration. Please let our tutors know if they should set up a private repository for you on GitHub. Please submit the *.py file and screenshots of your application to our personal addresses at the top of the sheet (not the mailing list) before the class on Thursday, 23.11.2017. Make sure to include your name and email address in both files. Please avoid compressing the files together. Use the following format for naming the files “cslsi-xx-lastName.pdf ” and “cslsi-xx-lastName.py”, where xx is the ID of assignment sheet. For example, for Alice Smith, the file name must be cslsi-06-smith.pdf.
Exercise 1 (GUI Programming, 25 Points)
Figure 1: The main window with the list widget.
In this exercise, you will develop a small GUI application that shows a list of subjects with their personal data, and allows the user to interactively edit it. We provide a scaffold list.py on the course webpage that you may use as a starting point. It defines ListWindow as a class for the main window, containing a member list of type QListWidget to show the list of subjects. Buttons underneath should allow editing. The personal data (name, year of birth, gender and symptoms) is stored in the member variable ListWindow.data.
• Implement the function updateList() in the class ListWindow. It should populate list with the subject names currently stored in data. This requires the use of QListWidget.addItem() for each subject. Since updateList() might get called repeatedly, you have to QListWidget.clear() all previous entries from list first.
• When the user clicks on the button Edit, we want the function onEditClicked() to be called. In the Qt library you can set a function handling the clicked signal via some_button.clicked.connect(some_function).
• Add a new function onDeleteClicked() to ListWindow, that deletes the subject in data that is currently selected by the user in list. Don’t forget to also repopulate list.
• Now it is time to implement the SubjectDialog. It should present the detailed information of a single subject. For this we need to add GUI elements in the constructor SubjectDia- log.__init__(). We want to be able to edit the subject’s name (text), the year of birth (number), gender (multiple choice) and symptoms (again, text).
Figure 2: The dialog box for the subject data.
– Create two new QLineEdits for the name and symptoms respectively. (only creating them won’t make them visible on the dialog box, we will take care of that later with the help of layouts)
– Create a new QSpinBox for the year of birth. Set an appropriate range of allowed values with setRange().
– For the gender, create 3 new QRadioButtons (’m’, ’f ’ and ’ ?’ ). Since only one choice should be possible simultaneously, we also need to create a QButtonGroup and add the 3 radio buttons to it.
– Create two buttons for Ok and Cancel. Qt already provides default handlers for both (self.accept and self.reject), but you still need to connect them to the buttons.
– Create a suitable layout. Use a QHBoxLayout to arrange the two buttons in a horizontal row. QFormLayout allows to stack the other widgets vertically and automatically add labels next to them (’Name’ etc.). Stack these two layouts vertically using a QVBoxLayout and apply it to the dialog via self.setLayout(). Hint: layouts in Qt have separate functions addWidget() and addLayout() to add child GUI elements directly or another child layout.
– We need to transport our subject’s data in and out of the dialog. For this we need two new functions getData() and setData() in SubjectDialog. setData(subject) should receive the data of a single subject (as a list) and fill out the appropriate GUI elements (for example QLineEdit.setText() and QRadioButton.setChecked()). getData() should read out the GUI elements (for example QLineEdit.text()), put the data into a list and return it.
• We can now also add a function in ListWindow to handle clicking the Add button. Similarly to onEditClicked() this new function should also show the SubjectDialog but add the result to data instead of replacing an existing element.
• Add two new buttons Load and Save to the main window that let the user choose a file (via QFileDialog.getOpenFileName() and QFileDialog.getSaveFileName()) and load/store data to the chosen file. Hint: to store the subject list into a file, you can use your knowledge from previous assignments, but in this case, it is also permitted to use the pandas library, python’s pickle mechanism or other methods you might prefer.
Hint: For a more detailed introduction to Python, you could refer to https://docs.python.org/3/ tutorial/. Unfortunately, PyQt does not come with a good reference documentation. Please refer to the Qt C++ documentation http://doc.qt.io/qt-5.6/reference-overview.html, and/or the manual of the PyQt competitor PySide http://srinikom.github.io/pyside-docs/PySide/QtGui/ index.html which has an almost identical API.
Good Luck!