Show Menu
Cheatography

TKinter Cheat Sheet (DRAFT) by

TKinter cheat sheet for simple games

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Libraries

# Tkinter
import tkinter as tk
# Image
from PIL import Image, ImageTk
# Random
import random
# Winsound
import winsound as ws

Window

# Create the window
root = tk.Tk()
# Change window size
root.geometry("1920x1080")
# Change window title
root.title("Title")
# Put the window in fullscreen
root.attributes("-fullscreen", True)

Widgets

# Frame
frame = tk.Frame(window, width=1920, height=1080)
# Canvas
canvas = tk.Canvas(frame)
# Label
label = tk.Label(frame, text="", font=("Arial", 50))
# Button
button = tk.Button(frame, text="", font=("Arial", 50),
         command=doSomething)
Width and height parameters are optional. If not specified, the widget will automa­tically take the size of its content.

Don't forget to pack or place your widget or they will not appear!
 

Basic Shapes

# Rectangle
rect_id = canvas.create_rectangle(x1, y1, x2, y2, fill="FFFF00")
# Oval
oval_id = canvas.create_oval(x1, y1, x2, y2, fill="red")
# Line
line_id = canvas.create_line(x1, y1, x2, y2)
Fill is optional. If not specified, the shape will be filled with white and a black outline.

Canvas Functions

# Modify properties
canvas.itemconfig(tagOrID, attributeName=newValue)
# Move widget
canvas.move(tagOrID, x1, y1)
# Delete everything inside the canvas
canvas.delete("all")
# Delete one widget
canvas.delete(tagOrID)
 

Image Library

# Load image
img = tk.PhotoImage(file="filename")
# Stock image into a Label
lbl_img = tk.Label(frame, image=img)