Skip to content

Exercises

Instructions

  • Do not use AI or genAI for help
  • Use ttk components whenever possible

Series 1

  1. Create a window that displays a button with a random number between 1 and 100 as text. Each time the button is clicked, the random number changes.
  2. Create a window that displays a "Quit" button and an editable text. Handle the following behavior when clicking the "Quit" button:
    • If the text hasn't been modified, the window closes,
    • If the text has been modified, a dialog box asks if you really want to quit. If you answer "Yes", the window closes, otherwise it stays open (see tip below).
  3. Create a window that displays an editable text and "save" and "load" buttons. Handle the following behavior:
    • When clicking "save", the text is saved to a text file,
    • When clicking "load", the text is loaded from the text file and displayed in the editable area (see tip below).
  4. Create a window that displays two editable texts and "save" and "load" buttons. Handle the following behavior:
    • When clicking "save", the text from the first editable area is saved to a text file. The filename is determined by the text in the second editable area,
    • When clicking "load", the text is loaded from the text file and displayed in the editable area. The filename is determined by the text in the second editable area.

Tips

Generate a random number
import random
number = random.randint(1, 100)
Close a tk window
window.destroy()
message box
import tkinter.messagebox as mb
answer = mb.askyesno(title="Confirm?", message="Do you want to quit?")
Save and load a file
def save(text):
    with open("file.txt", "w") as f:
        f.write(text)

def load():
    with open("file.txt", "r") as f:
        return f.read()
Display an image from file with tkinter
from tkinter import PhotoImage

img = PhotoImage(file="image.png")
label = Label(window, image=img)
label.pack()

Solutions

Random number button
import random
from playsound import playsound
import tkinter as tk
from tkinter import ttk

window = tk.Tk()
window.geometry(200, 200)

button: ttk.Button = None


def get_rand_int_as_str() -> str:
    r = random.randint(1, 100)
    return str(r)


def change_label():
    if button == None:
        return
    button.configure(text=get_rand_int_as_str())


button = ttk.Button(window, text=get_rand_int_as_str(), command=change_label)

button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
window.mainloop()
Confirm quit
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as mb

window = tk.Tk()
window.geometry("200x200")

entered_text = tk.StringVar()
textbox = ttk.Entry(window, textvariable=entered_text)
textbox.pack(pady=50)


def quit_app():
    if len(entered_text.get()) == 0:
        window.destroy()
        return
    anwser = mb.askyesno(title="Confirmer", message="Voulez-vous quitter ?")
    if anwser:
        window.destroy()
        return


run_button = ttk.Button(window, text="Quit 👋", command=quit_app)
run_button.pack()

window.mainloop()

Series 2

  1. Create a window that displays an image from a file.
  2. Create a window that displays a text input area and a button. When the user clicks the button, the image whose name is defined by the input area is displayed below.
  3. Using the playsound library, create a window that plays a sound when clicking a button.
  4. Use tkvideoplayer to create a window that plays a video.

Series 2 Solutions

Play music
# importing required module
from playsound import playsound
import tkinter as tk
from tkinter import ttk

window = tk.Tk()
window.title("GeeksforGeeks sound player")  # giving the title for our window
window.geometry("500x400")


# making function
def play():
    playsound("sample-3s.mp3")


# title on the screen you can modify it
title = ttk.Label(
    window,
    text="Sample sound",
)
title.pack()

# making a button which trigger the function so sound can be playeed
play_button = ttk.Button(window, text="Play Song", command=play)
play_button.pack()

info = ttk.Label(window, text="Click on the button above to play song ").pack(pady=20)
window.mainloop()

Series 3

  1. Using tkinter.scrolledtext, create an application that allows text input, saving to a file, and loading.
  2. Create an application that allows text input, saving to a file, and loading. It also remembers the last modification and allows restoring it using the "Undo" button. If the modification is restored, the "Undo" button becomes "Redo" and allows restoring the modification.
  3. Modify the application to remember the last 10 modifications, the "Undo" button allows going backwards, and the "Redo" button allows going forwards. The behavior should be similar to classic text editors.
  4. Associate keyboard shortcuts with each button. For example, Ctrl+S for save, Ctrl+Z for undo, Ctrl+Y for redo, etc.