A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 14 Solution

  • Course instructor: David Dumas
  • Solutions prepared by: Johnny Joyce

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 30, 2021.

Topic

This homework assignment focuses on tkinter and working with dates and times.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials and other resources you are allowed to refer to for this homework are:

(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)

I know project 4 is underway and this homework spans a holiday break

This homework is correspondingly short.

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. Thus the grading breakdown is:

Points Item
2 Autograder
4 Problem 2
6 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Counter

Make a GUI counter application that has a single button labeled "Increment", and a text label next to it. The text label should initially read 0, and every time the button is pressed, the number on the label should increase by one. A sample of what using the program should look like is shown below.

OOP optional

You can choose whether to do this by subclassing tkinter.Tk (which is recommended in general) or by building all the widgets in the main program (as in the first lecture demonstration of GUI programming).

Restricted methods note

  • The only module(s) you are allowed to import are: tkinter and tkinter.ttk
  • You can only use Python syntax and techniques we've covered in MCS 260
In [ ]:
'''Creates a tkinter GUI with a button and a counter for how many times the button has been pressed'''

import tkinter
import tkinter.ttk


class IncrementGUI(tkinter.Tk):
    '''A class for a tkinter window'''
    
    def __init__(self):
        '''Initialize: create a button and a label'''
        super().__init__()
        
        self.count = 0
        
        self.title("Counter")
        
        self.increment_button = tkinter.ttk.Button(self, text = "Increment", command = self.on_click)
        self.increment_button.grid(row=0,column=0,padx=5,pady=5,ipadx=10,ipady=10)
        
        self.counter = tkinter.ttk.Label(self, text = self.count)
        self.counter.grid(row=0,column=1,padx=5,pady=5,ipadx=10,ipady=10)
        
    def on_click(self):
        '''Runs when button is pressed. Increases number on label'''
        
        self.count = self.count + 1
        self.counter.config(text=self.count)
        

window = IncrementGUI()
window.mainloop()

Congratulations

You've just completed the last homework assignment of MCS 260.

(All that remains is Project 4!)

Revision history

  • 2021-11-30 Initial upload of solutions
  • 2021-11-30 Added docstrings