User Inputs from Tkinter to Create ID Card


Interactive Google Colab Practice Notebook

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 Open In Colab
User inputs from Tkinter for ID card

Generating PDF ID card by using user input data & image through Tkinter window using ReportLab

Tkinter Window to Take User Inputs 🔝

We need five input fields from the user to create the ID card:

  • s_name: Student name
  • s_id: Student ID
  • s_class: Student class
  • s_gender: Student gender
  • s_filename: File path of the student's photo

We 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.

GUI Input Module: tk_id_input.py

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()

Creating the ID Card Using Inputs 🔝

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.

Main File: tk_id_card_main.py

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}")

Template File: tk_id_card_temp.py

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

Practical Uses of Interactive Desktop PDF Generation

Integrating Desktop GUI frameworks (like Tkinter, PyQt, or CustomTkinter) with ReportLab provides several operational benefits:

  • Desktop Form-to-PDF Workflows: Allow office staff to type student metadata and browse local photos without needing full web server setups.
  • On-Demand Student Registration: Instantly preview and generate official student ID cards right at administrative desks upon enrollment.
  • Input Validation & File Browsing: Enforce valid input formats and file extension filtering (JPEG, PNG) using standard OS file dialogs before invoking ReportLab builders.

Common Errors & Troubleshooting

  • Tkinter Event Loop Blocking: Placing long-running ReportLab generation code directly inside the main GUI thread without destroying or hiding the Tk window can freeze the UI. Always trigger destruction or handle generation asynchronously.
  • Reading Unassigned File Paths: If the user submits the form before selecting an image file using filedialog.askopenfilename(), `s_filename` remains an empty string. Wrap `c.drawImage` calls in a check for valid non-empty file paths.
  • Accessing `StringVar` Directly: Passing `s_id` directly to `c.drawString()` passes the Tkinter variable object rather than the string value. Always call `.get()` (e.g. `str(s_id.get())`).

Frequently Asked Questions (FAQ)

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.

Further Reading & Related Tutorials


PDF ID card using Template
PDF Mark sheet by using data from SQLite database Generate PDF Invoice using Sales data Table from different data sources to PDF file Table from Pandas DataFrame to PDF file PDF Circles & Shapes
Create Report in PDF using Charts, images, tables and Paragraphs
Bar charts in PDF Line charts in PDF Pie Charts Paragraph
Python



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer