cs110_ex7
1from tkinter import Tk, Canvas, messagebox, simpledialog 2 3CORRECT_COLOR = "#6BA965" 4PARTIAL_COLOR = "#C8B458" 5WRONG_COLOR = "#787C7E" 6DEFAULT_COLOR = "white" 7KEY_PRESS = "<Key>" 8 9__docformat__ = "google" 10 11canvas = None 12 13def make_grid(w, h): 14 """ 15 Makes a grid on a screen. 16 17 Args: 18 w (`int`): The width of the screen 19 h (`int`): The height of the screen 20 21 Returns: 22 * `None`, draws to the screen 23 """ 24 interval = 100 25 26 # Delete old grid if it exists: 27 canvas.delete("grid_line") 28 # Creates all vertical lines at intevals of 100 29 for i in range(0, w, interval): 30 canvas.create_line(i, 0, i, h, tag="grid_line", fill="black") 31 32 # Creates all horizontal lines at intevals of 100 33 for i in range(0, h, interval): 34 canvas.create_line(0, i, w, i, tag="grid_line", fill="black") 35 36 37def draw_letter_in_grid(letter, grid_coord, past_guess=False): 38 """ 39 Draws letters on a window in a particular grid coordinate. 40 41 Args: 42 letter (`str`): The letter we wish to draw on the canvas 43 grid_coord (`tuple`): An x,y coordinate that says where to draw the letter 44 past_guess (`bool`): If true, then this is a previous guess (otherwise current guess) 45 46 Returns: 47 * `None`, draws to the scren 48 """ 49 text_color = "black" 50 if past_guess: 51 text_color = "white" 52 53 canvas.create_text( 54 (grid_coord[0] * 100) + 50, 55 (grid_coord[1] * 100) + 50, 56 text=letter.upper(), 57 anchor="center", 58 font=("Purisa", 100), 59 fill=text_color 60 ) 61 62 63def game_over(happy = False): 64 """ 65 Ends the game. 66 67 Args: 68 happy (`bool`): If this is true, that means the user has won! 69 70 Returns: 71 * `None`, maybe shows a pop-up window and possibly stops listening for keyboard events. 72 """ 73 if happy: 74 messagebox.showinfo("Congratulations", "You"ve won!") 75 canvas.unbind(KEY_PRESS) 76 77 78def color_a_grid_square(color, grid_coord): 79 """ 80 Colors a particular grid square on a canvas. 81 82 Args: 83 color (`str`): The color to draw the square in 84 grid_coord (`tuple`): The x,y coordinate of the square we wish to draw 85 86 Returns: 87 * `None`, draws to the screen. 88 """ 89 top_left = (grid_coord[0] * 100, grid_coord[1] * 100) 90 bottom_right = (top_left[0] + 100, top_left[1] + 100) 91 canvas.create_rectangle( 92 top_left, 93 bottom_right, 94 fill=color, 95 outline="black" 96 ) 97 98 99def delete(shape): 100 """ 101 A function that deletes a shape from our screen. 102 103 Args: 104 shape (`Shape` or Tag): The shape to delete. 105 """ 106 canvas.delete(shape) 107 108def setup_listener(event, handler_function): 109 """ 110 Sets up a listener for a given event on our window. 111 112 Args: 113 event (`str`): The magic string that represents this event in tkinter 114 handler_function (`func`): The name (not a string though) of the function you want called when the event his heard. 115 """ 116 canvas.bind("<Key>", handler_function) 117 canvas.focus_set() 118 119def fix_file_path(file_path): 120 """ 121 Only use this function if you"re using an IDE that is NOT IDLE. 122 123 Args: 124 file_path (`str`): A path to a file you want to use. 125 126 Returns: 127 * An augmented file path (`str`) to fix issues with non-IDLE IDEs. 128 """ 129 import os 130 import sys 131 dir_path = os.path.dirname(sys.argv[0]) 132 return os.path.join(dir_path, file_path) 133 134def setup_window(some_canvas): 135 """DO NOT USE THIS FUNCTION""" 136 global canvas 137 canvas = some_canvas 138 canvas.pack() 139 canvas.focus_set()
CORRECT_COLOR =
"#6BA965"
PARTIAL_COLOR =
"#C8B458"
WRONG_COLOR =
"#787C7E"
DEFAULT_COLOR =
"white"
KEY_PRESS =
"<Key>"
canvas =
None
def
make_grid(w, h):
14def make_grid(w, h): 15 """ 16 Makes a grid on a screen. 17 18 Args: 19 w (`int`): The width of the screen 20 h (`int`): The height of the screen 21 22 Returns: 23 * `None`, draws to the screen 24 """ 25 interval = 100 26 27 # Delete old grid if it exists: 28 canvas.delete("grid_line") 29 # Creates all vertical lines at intevals of 100 30 for i in range(0, w, interval): 31 canvas.create_line(i, 0, i, h, tag="grid_line", fill="black") 32 33 # Creates all horizontal lines at intevals of 100 34 for i in range(0, h, interval): 35 canvas.create_line(0, i, w, i, tag="grid_line", fill="black")
Makes a grid on a screen.
Arguments:
- w (
int
): The width of the screen - h (
int
): The height of the screen
Returns:
None
, draws to the screen
def
draw_letter_in_grid(letter, grid_coord, past_guess=False):
38def draw_letter_in_grid(letter, grid_coord, past_guess=False): 39 """ 40 Draws letters on a window in a particular grid coordinate. 41 42 Args: 43 letter (`str`): The letter we wish to draw on the canvas 44 grid_coord (`tuple`): An x,y coordinate that says where to draw the letter 45 past_guess (`bool`): If true, then this is a previous guess (otherwise current guess) 46 47 Returns: 48 * `None`, draws to the scren 49 """ 50 text_color = "black" 51 if past_guess: 52 text_color = "white" 53 54 canvas.create_text( 55 (grid_coord[0] * 100) + 50, 56 (grid_coord[1] * 100) + 50, 57 text=letter.upper(), 58 anchor="center", 59 font=("Purisa", 100), 60 fill=text_color 61 )
Draws letters on a window in a particular grid coordinate.
Arguments:
- letter (
str
): The letter we wish to draw on the canvas - grid_coord (
tuple
): An x,y coordinate that says where to draw the letter - past_guess (
bool
): If true, then this is a previous guess (otherwise current guess)
Returns:
None
, draws to the scren
def
game_over(happy=False):
64def game_over(happy = False): 65 """ 66 Ends the game. 67 68 Args: 69 happy (`bool`): If this is true, that means the user has won! 70 71 Returns: 72 * `None`, maybe shows a pop-up window and possibly stops listening for keyboard events. 73 """ 74 if happy: 75 messagebox.showinfo("Congratulations", "You"ve won!") 76 canvas.unbind(KEY_PRESS)
Ends the game.
Arguments:
- happy (
bool
): If this is true, that means the user has won!
Returns:
None
, maybe shows a pop-up window and possibly stops listening for keyboard events.
def
color_a_grid_square(color, grid_coord):
79def color_a_grid_square(color, grid_coord): 80 """ 81 Colors a particular grid square on a canvas. 82 83 Args: 84 color (`str`): The color to draw the square in 85 grid_coord (`tuple`): The x,y coordinate of the square we wish to draw 86 87 Returns: 88 * `None`, draws to the screen. 89 """ 90 top_left = (grid_coord[0] * 100, grid_coord[1] * 100) 91 bottom_right = (top_left[0] + 100, top_left[1] + 100) 92 canvas.create_rectangle( 93 top_left, 94 bottom_right, 95 fill=color, 96 outline="black" 97 )
Colors a particular grid square on a canvas.
Arguments:
- color (
str
): The color to draw the square in - grid_coord (
tuple
): The x,y coordinate of the square we wish to draw
Returns:
None
, draws to the screen.
def
delete(shape):
100def delete(shape): 101 """ 102 A function that deletes a shape from our screen. 103 104 Args: 105 shape (`Shape` or Tag): The shape to delete. 106 """ 107 canvas.delete(shape)
A function that deletes a shape from our screen.
Arguments:
- shape (
Shape
or Tag): The shape to delete.
def
setup_listener(event, handler_function):
109def setup_listener(event, handler_function): 110 """ 111 Sets up a listener for a given event on our window. 112 113 Args: 114 event (`str`): The magic string that represents this event in tkinter 115 handler_function (`func`): The name (not a string though) of the function you want called when the event his heard. 116 """ 117 canvas.bind("<Key>", handler_function) 118 canvas.focus_set()
Sets up a listener for a given event on our window.
Arguments:
- event (
str
): The magic string that represents this event in tkinter - handler_function (
func
): The name (not a string though) of the function you want called when the event his heard.
def
fix_file_path(file_path):
120def fix_file_path(file_path): 121 """ 122 Only use this function if you"re using an IDE that is NOT IDLE. 123 124 Args: 125 file_path (`str`): A path to a file you want to use. 126 127 Returns: 128 * An augmented file path (`str`) to fix issues with non-IDLE IDEs. 129 """ 130 import os 131 import sys 132 dir_path = os.path.dirname(sys.argv[0]) 133 return os.path.join(dir_path, file_path)
Only use this function if you"re using an IDE that is NOT IDLE.
Arguments:
- file_path (
str
): A path to a file you want to use.
Returns:
- An augmented file path (
str
) to fix issues with non-IDLE IDEs.
def
setup_window(some_canvas):
135def setup_window(some_canvas): 136 """DO NOT USE THIS FUNCTION""" 137 global canvas 138 canvas = some_canvas 139 canvas.pack() 140 canvas.focus_set()
DO NOT USE THIS FUNCTION