utils/prog/tqr.py

67 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
QR Code creation from clipboard.
Test this program simply with pipx:
2024-08-11 21:41:13 +00:00
pipx run https://git.sekun.eu/sekun/utils/raw/branch/main/prog/tqr.py
"""
# /// script
# dependencies = [
# "pyqrcode",
# ]
# ///
import tkinter as tk
from tkinter import messagebox
import pyqrcode as qr
def main():
root = tk.Tk()
root.title = "Qr code"
root.state("iconic")
2024-08-12 22:05:41 +00:00
try:
data = root.clipboard_get()
except Exception as e:
messagebox.showerror("Qr code", f"Cannot get content of clipboard.\n«{e}»")
exit(1)
try:
code = qr.create(data)
except ValueError as e:
messagebox.showerror(
"Qr code", f"Erreur lors de la création du code QR.\n«{e}»"
)
exit(1)
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
size = min(sw, sh) - 100
scale = size // (len(code.code[0]) + 8)
img = tk.BitmapImage(data=code.xbm(scale=scale))
w = img.width()
root.geometry(f"{w}x{w}+50+50")
img.config(background="white")
label = tk.Label(root, image=img)
label.pack()
2024-08-12 22:05:41 +00:00
def empty_clipboard():
root.clipboard_clear()
root.destroy()
root.protocol("WM_DELETE_WINDOW", empty_clipboard)
root.mainloop()
if __name__ == "__main__":
main()