The Canvas
widget is Tkinter's primary widget for displaying graphics. With a vast range of built-in functions for creating graphics manually, it is the perfect choice for the display piece of a computer game.
Let's have a quick introduction to the Canvas
widget's built-in drawing capabilities. Open up a new file and type in the following:
import tkinter as tk window = tk.Tk() canvas = tk.Canvas(window, bg="white", width=300, height=300) canvas.pack() canvas.create_oval((0, 0, 300, 300), fill="yellow") canvas.create_arc((50, 100, 100, 150), extent=180, fill="black") canvas.create_arc((200, 100, 250, 150), extent=180, fill="black") canvas.create_line((50, 200, 110, 240), fill="red", width=5) canvas.create_line((110, 240, 190, 240), fill="red", width=5) canvas.create_line((190, 240, 250, 200), fill="red", width=5) window.mainloop()
Run this code and you should see a nice smiley face appear on your computer screen:
Let's go over exactly how we create this image:
- We begin...