280 likes | 292 Views
Computer Science 111. Fundamentals of Programming I List boxes and dialogs. The Model/View Pattern. View (User Interface). Data Model. The data model consists of software components that manage a system’ s data (game logic, tax code, etc.)
E N D
Computer Science 111 Fundamentals of Programming I List boxes and dialogs
The Model/View Pattern View (User Interface) Data Model The data model consists of software components that manage a system’s data (game logic, tax code, etc.) The view consists of software components that allow human users to view and interact with the data model
Example: The Counter App View (User Interface) Data Model CounterGUI Counter
Example: The Number Guess App View (User Interface) Data Model NumberGuessGUI NumberGuess
Example: The Tax Code App View (User Interface) Data Model TaxCodeGUI TaxCode
Another Example • A simple-minded course management system calls for representing students’ information • Each student has a name and a set of exam scores • There can be zero or more students
Data Modeling:Composition and Aggregation * int list Student str means “is composed of” * means “aggregates zero or more within” A student’s name is a string, and a list holds the student’s scores
Data Modeling:Composition and Aggregation * Student dict Roster means “is composed of” * means “aggregates zero or more within” The roster has a single dictionary, and the dictionary has zero or more students, keyed by names
Behavior of a Student s = Student(scores = 3) # Creates and returns a student with # the given number of scores s.getNumberOfScores() # Returns the number of scores s.getName() # Returns the student’s name s.setName(newName) # Sets student’s name to newName s.getScore(i) # Returns the score at position i s.setScore(i, newScore) # Sets score at position i to newScore s.getAverage() # Returns the student’s average score s.getHighScore() # Returns the student’s high score str(s) # Returns the string rep of the student
Behavior of the Roster model = Roster(numberOfScores = 3) model.getNumberOfScores() # Returns the number of scores model.getNames() # Returns a list of student names model.add(s) # Adds s to the roster model.remove(name) # Removes the student with the name model.get(name) # Returns the student with the name len(model) # Returns the number of students str(model) # Returns the string rep of the roster
Two Views of a Student StudentGUI1 Student StudentGUI2
StudentGUI1 from breezypythongui import EasyFrame classStudentGUI1(EasyFrame): """Displays the student's info in a text area.""" def__init__(self, student): """Sets up the window.""" EasyFrame.__init__(self, title = "Student") # Instance variable to track the student. self.student = student # A text area to display the student's info self.addTextArea(text = str(self.student), row = 0, column = 0, width = 25))
GUI Tradeoffs • GUI 1 is easy for viewing • GUI 2 is easy for editing • Use GUI 1 for viewing, and pop up a new window (a dialog) for making changes
Popup Dialogs • Like a window (can display all the widgets, handle events, etc.) • Takes control until closed • The user can click OK to accept changes or Cancel toback out • Define as a subclass of EasyDialog
StudentGUI3 from breezypythongui import EasyFrame, EasyDialog classStudentGUI3(EasyFrame): """Displays the student's info in a text area.""" def__init__(self, student): """Sets up the window.""" EasyFrame.__init__(self, title = "Student") # Instance variable to track the student. self.student = student # A text area to display the student's info. self.textArea = self.addTextArea(text = str(self.student), row = 0, column = 0, width = 25)) # A text area to to modify the Student object. self.addButton(text = "Modify", row = 1, column = 0, command = self.modify)
StudentGUI3 from breezypythongui import EasyFrame, EasyDialog classStudentGUI3(EasyFrame): """Displays the student's info in a text area.""” . . . # Event-handling method for the Modify button. defmodify(self): """Pops up a dialog to modify the student's info. Updates the text area if the user clicks OK.""" dialog = StudentDialog(self, self.student) if dialog.modified(): self.textArea.setText(str(self.student)) Pass the data model to the dialog, just like you do to the top-level window
Two Views of a Student StudentGUI3 Student StudentDialog StudentGUI3 is the main window StudentDialog is the popup dialog Student is the data model
The Structure of a Dialog Class • __init__: sets up the parent class and saves a reference to the data model • body: creates and lays out the widgets • apply: the event handler for the OK button
StudentGUI3 from breezypythongui import EasyFrame, EasyDialog classStudentDialog(EasyDialog): """Opens a dialog on a student object.""” def__init__(self, parent, student): """Sets up the window.""" self.student = student EasyDialog.__init__(self, parent, "Student Editor")
StudentGUI3 - body from breezypythongui import EasyFrame, EasyDialog classStudentDialog(EasyDialog): """Opens a dialog on a student object.""" defbody(self, master): """Sets up the widgets.""" self.addLabel(master, text = "Name", row = 0, column = 0) self.nameField = self.addTextField(master, text = self.student.getName(), row = 0, column = 1) #Labels and fields for the student's scores self.scoreFieldList = list() for index inrange(self.student.getNumberOfScores()): self.addLabel(master, text = "Score " + str(index + 1), row = index + 1, column = 0) field = self.addIntegerField(master, value = self.student.getScore(index), row = index + 1, column = 1) self.scoreFieldList.append(field)
StudentGUI3 - apply from breezypythongui import EasyFrame, EasyDialog classStudentDialog(EasyDialog): """Opens a dialog on a student object.""" def apply(self): """When the OK button is clicked, transfers data from the fields to the song.""" if self.nameField.getText() == "": self.messageBox("ERROR", "Name is missing") return self.student.setName(self.nameField.getText()) for index inrange(self.student.getNumberOfScores()): self.student.setScore(index, self.scoreFieldList[index].getNumber()) self.setModified()
The GUI for the Roster List box Text area
The Structure of the Roster App RosterGUI Roster StudentDialog Student RosterGUI is the main window class StudentDialog is the popup dialog class Student and Roster are the data model classes
RosterApp from roster import createRoster from rostergui import RosterGUI defmain(): """Instantiates the model and the view, and opens the window.""” model = createRoster(3) view = RosterGUI(model) view.mainloop() # Entry point of the application. if __name__ == "__main__": main() createRoster is a helper function that creates several Student objects and adds them to a roster for testing
List Boxes • Hold zero or more strings • Can respond to a user’s selection of an item • Can add or remove items, track the list’s length, etc.
For Friday Read Chapter 2 on Collections