Created
March 31, 2025 07:16
-
-
Save limafresh/7e267a86dfc5bf50f7816aa4b1d57e45 to your computer and use it in GitHub Desktop.
Tkinter GUI QR code scanner using pyzbar library and supporting loading codes from image and pasting from clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Author: limafresh <https://github.com/limafresh> | |
License: CC0 <https://creativecommons.org/publicdomain/zero/1.0/> | |
""" | |
import tkinter as tk | |
from tkinter import filedialog, ttk | |
from PIL import Image, ImageTk, ImageGrab | |
from pyzbar.pyzbar import decode | |
def upload_image(): | |
file_path = filedialog.askopenfilename() | |
if file_path: | |
try: | |
global img | |
img = Image.open(file_path) | |
scan_code() | |
except Exception as e: | |
error() | |
tk.messagebox.showinfo("Error", e) | |
def paste_image(): | |
try: | |
global img | |
img = ImageGrab.grabclipboard() | |
scan_code() | |
except Exception as e: | |
error() | |
tk.messagebox.showinfo("Error", e) | |
def scan_code(): | |
image_canvas.delete("all") | |
img_resized = img.resize((image_canvas.winfo_width(), image_canvas.winfo_height())) | |
img_tk = ImageTk.PhotoImage(img_resized) | |
image_canvas.create_image(0, 0, anchor=tk.NW, image=img_tk) | |
image_canvas.image = img_tk | |
decoded_data = decode(img) | |
if decoded_data: | |
global link | |
link = decoded_data[0].data.decode("utf-8") | |
copy_button.config(state=tk.NORMAL) | |
link_entry.config(state=tk.NORMAL) | |
link_entry.delete(0, tk.END) | |
link_entry.insert(0, link) | |
else: | |
error() | |
tk.messagebox.showinfo("Error", "Failed to recognize QR code") | |
def error(): | |
image_canvas.delete("all") | |
link_entry.delete(0, tk.END) | |
copy_button.config(state=tk.DISABLED) | |
link_entry.config(state=tk.DISABLED) | |
def copy_to_clipboard(): | |
root.clipboard_clear() | |
root.clipboard_append(link) | |
root.update() | |
root = tk.Tk() | |
root.title("QR code scanner") | |
upload_frame = ttk.Frame(root) | |
upload_frame.pack(padx=10, pady=10) | |
upload_button = ttk.Button(upload_frame, text="Upload QR image", command=upload_image) | |
upload_button.grid(row=1, column=1, padx=10, pady=10) | |
paste_button = ttk.Button(upload_frame, text="Paste QR image", command=paste_image) | |
paste_button.grid(row=1, column=2, padx=10, pady=10) | |
image_canvas = tk.Canvas( | |
root, | |
width=300, | |
height=300, | |
bg="white", | |
highlightthickness=2, | |
highlightbackground="blue", | |
) | |
image_canvas.pack(padx=10, pady=10) | |
link_frame = ttk.Frame(root) | |
link_frame.pack(padx=10, pady=10) | |
link_label = ttk.Label(link_frame, text="Link") | |
link_label.grid(row=1, column=1, padx=10, pady=10) | |
link_entry = ttk.Entry(link_frame, state=tk.DISABLED) | |
link_entry.grid(row=1, column=2, padx=10, pady=10) | |
copy_button = ttk.Button( | |
link_frame, text="Copy", command=copy_to_clipboard, state=tk.DISABLED | |
) | |
copy_button.grid(row=1, column=3, padx=10, pady=10) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment