Practice building interactive Tkinter forms, selecting photo files, passing GUI inputs (`StringVar`), and generating PDF student ID cards with ReportLab directly inside Google Colab.
View on GitHub
We need five input fields from the user to create the ID card:
s_name: Student names_id: Student IDs_class: Student classs_gender: Student genders_filename: File path of the student's photoWe use tk.StringVar() to capture form inputs and pass them to the main canvas script. The submit button destroys the window instance via command=lambda: my_w.destroy() to release execution control to the canvas builder.
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
my_w = tk.Tk()
my_w.geometry("400x350")
s_name = tk.StringVar(value='Kalu')
s_id = tk.StringVar(value='16')
s_class = tk.StringVar(value='Four')
s_gender = tk.StringVar(value='Female')
s_filename = ''
l1 = tk.Label(my_w, text='Name', font=22, width=8, fg='blue')
l1.grid(row=0, column=0, padx=1, pady=10)
e1 = tk.Entry(my_w, font=22, width=8, textvariable=s_name)
e1.grid(row=0, column=1, padx=1, pady=10)
l2 = tk.Label(my_w, text='ID', font=22, width=8, fg='blue')
l2.grid(row=0, column=2, padx=1, pady=10, sticky='w')
e2 = tk.Entry(my_w, font=22, textvariable=s_id, width=8)
e2.grid(row=0, column=3, padx=1, pady=10, sticky='w')
l3 = tk.Label(my_w, text='Class', font=22, width=8, fg='blue')
l3.grid(row=1, column=0, padx=1, pady=10)
e3 = tk.Entry(my_w, font=22, textvariable=s_class, width=8)
e3.grid(row=1, column=1, padx=1, pady=10)
l4 = tk.Label(my_w, text='Gender', font=22, width=8, fg='blue')
l4.grid(row=1, column=2, padx=1, pady=10, sticky='w')
e4 = tk.Entry(my_w, font=22, textvariable=s_gender, width=8)
e4.grid(row=1, column=3, padx=1, pady=10, sticky='w')
b1 = tk.Button(my_w, text='Select Image', font=22, command=lambda: upload_file())
b1.grid(row=2, column=0, columnspan=2)
def upload_file():
global img, s_filename
f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
s_filename = filedialog.askopenfilename(filetypes=f_types)
if s_filename:
img = ImageTk.PhotoImage(file=s_filename)
b4 = tk.Button(my_w, image=img)
b4.grid(row=2, column=2, columnspan=2)
b2 = tk.Button(my_w, text='Submit', font=22, command=lambda: my_w.destroy())
b2.grid(row=3, column=1, padx=10, pady=10)
my_w.mainloop()
We import the canvas template from ID Card Part 1 to maintain layout consistency:
from tk_id_card_temp import my_temp # import the template
from tk_id_input import s_name, s_id, s_class, s_gender, s_filename # Tkinter inputs
Since these variables are StringVar() instances, we call their get() method to retrieve string inputs before passing them into ReportLab draw methods.
from pathlib import Path
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from tk_id_card_temp import my_temp
from tk_id_input import s_name, s_id, s_class, s_gender, s_filename
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'tkinter_id_card.pdf'
c = canvas.Canvas(str(my_path), pagesize=(400, 300))
c = my_temp(c)
if s_filename:
c.drawImage(s_filename, 2.2 * inch, 0.7 * inch)
c.setFillColorRGB(0, 0, 1)
c.setFont("Helvetica", 20)
c.drawString(0.5 * inch, 1.7 * inch, str(s_id.get()))
c.drawString(0.5 * inch, 1.3 * inch, s_name.get())
c.drawString(0.5 * inch, 0.9 * inch, s_class.get())
c.drawString(0.5 * inch, 0.5 * inch, s_gender.get())
c.showPage()
c.save()
print(f"ID Card generated: {my_path}")
from reportlab.lib.units import inch
def my_temp(c):
c.translate(inch, inch)
c.setFont("Helvetica", 14)
c.setStrokeColorRGB(0.1, 0.8, 0.1)
c.setFillColorRGB(0, 0, 1)
# Watermark
c.rotate(35)
c.setFillColorCMYK(0, 0, 0, 0.08)
c.setFont("Helvetica", 100)
c.drawString(-1.1 * inch, -0.5 * inch, "SAMPLE")
c.rotate(-35)
c.setFillColorRGB(1, 0, 0)
c.setFont("Helvetica", 25)
c.drawRightString(1.7 * inch, 2.3 * inch, 'Identity Card')
c.setFillColorRGB(0, 0, 0)
c.setFont("Helvetica", 24)
c.drawRightString(0.3 * inch, 1.7 * inch, 'ID:')
c.drawRightString(0.3 * inch, 1.3 * inch, 'Name:')
c.drawRightString(0.3 * inch, 0.9 * inch, 'Class:')
c.drawRightString(0.3 * inch, 0.5 * inch, 'Gender:')
c.drawRightString(4.0 * inch, -0.5 * inch, 'Signature')
c.line(-1.1, -0.7 * inch, 5 * inch, -0.7 * inch)
c.setFont("Helvetica", 8)
c.setFillColorRGB(1, 0, 0)
c.drawString(0, -0.9 * inch, "\u00A9 plus2net.com")
return c
Integrating Desktop GUI frameworks (like Tkinter, PyQt, or CustomTkinter) with ReportLab provides several operational benefits:
filedialog.askopenfilename(), `s_filename` remains an empty string. Wrap `c.drawImage` calls in a check for valid non-empty file paths.Q: How do I clear form fields after generating a PDF?
A: Set `s_name.set('')`, `s_id.set('')`, etc., inside a reset button callback instead of calling `my_w.destroy()`.
Q: Can I run Tkinter GUI applications in Google Colab?
A: Standard Tkinter GUI windows require a local X11 display. In headless environments like Google Colab, input form data can be simulated using Google Colab form widgets or Python dictionaries.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.