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

MCS 260 Fall 2021 Worksheet 14

  • Course instructor: David Dumas

Topics

This worksheet focuses on tkinter and working with dates and times.

Resources

The main resources to refer to for this worksheet 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.)

1. Tuesdays

Write a program that accepts two command line arguments, each of which is expected to be a date in the format YYYY-MM-DD, and which then prints the date of every Tuesday that occurs between the given dates.

I recommend doing this with a loop that starts at the first date and steps forward, one day at a time, checking each time whether the day of week is a Tuesday. There is a convenient method .weekday() of datetime.datetime objects that returns an integer, where 0 means Monday, 1 means Tuesday, etc..

Also, you could do this by parsing strings into datetime.datetime objects and then stepping forward by 24 hours at a time, but I suggest you convert the datetime.datetime objects to datetime.date objects so that the time of day is ignored. Each datetime.datetime object has a method .date() that converts it to a date, discarding the time information.

Sample usage:

PS C:\Users> python3 tuesdays.py 2021-08-23 2021-12-03
2021-08-24
2021-08-31
2021-09-07
2021-09-14
2021-09-21
2021-09-28
2021-10-05
2021-10-12
2021-10-19
2021-10-26
2021-11-02
2021-11-09
2021-11-16
2021-11-23
2021-11-30
PS C:\Users>

2. Whack-a-mole

You might be familiar with a popular carnival game called "Whack-a-mole" or "Mogura Taiji", where a player must use a mallet to strike small objects that pop up from an array of holes.

Make a tkinter GUI version of this game, where the holes are replaced by buttons, one of which is labeled "Mole" while the others say nothing.

Clicking on the mole button should make the mole move to a different location (at random). Clicking on any other button should end the game immediately.

If the user hits the mole 10 times in a row, the game should end and print a message congratulating the user, and telling them how many seconds it took to complete the game. The timing should begin when they press a button for the first time (rather than when the window opens).

A template program has been provided which takes care of some basic GUI setup:

It is recommended that you complete this problem by editing that program. The starter program also demonstrates the correct way to end a tkinter GUI application, which is to call the .quit() method on the tkinter.Tk object.

Revision history

  • 2021-11-21 Initial release