Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

make me a python gui QR code creator

  1. User

    make me a python gui QR code creator

  2. Assistant

    Certainly! To create a Python GUI QR code generator, you can use two primary libraries: tkinter for the graphical user interface, and qrcode for generating QR codes. If you haven't already installed the qrcode library, you can do so using pip:

    pip install qrcode[pil]
    

    Here’s a simple script to create a GUI QR code creator with Python:

    import tkinter as tk
    from tkinter import ttk, filedialog
    import qrcode
    from PIL import Image, ImageTk
    
    # Main application class
    class QRCodeApp(tk.Tk):
        def __init__(self):
            super().__init__()
    
            self.title("QR Code Generator")
            self.geometry("400x500")
    
            # Create and pack widgets
            self.create_widgets()
    
        def create_widgets(self):
            # Text input frame
            input_frame = ttk.Frame(self)
            input_frame.pack(pady=20)
    
            # Label for the text input
            ttk.Label(input_frame, text="Text for QR Code:").pack(side="left", padx=(0, 10))
    
            # Text input for the QR code content
            self.text_input = ttk.Entry(input_frame, width=20)
            self.text_input.pack(side="left")
            self.text_input.focus()
    
            # Button to generate QR code
            generate_button = ttk.Button(self, text="Generate QR", command=self.generate_qr)
            generate_button.pack(pady=10)
    
            # Canvas for QR code display
            self.canvas = tk.Canvas(self, width=350, height=350)
            self.canvas.pack()
    
        def generate_qr(self):
            # Get text and create QR code
            qr_text = self.text_input.get()
            if not qr_text:
                return  # Do nothing if text is empty
    
            qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L,
                               box_size=10, border=4,)
            qr.add_data(qr_text)
            qr.make(fit=True)
    
            img = qr.make_image(fill_color="black", back_color="white")
            
            # Save the QR code to a temporary file and display
            img_temp_path = "temp_qr.png"
            img.save(img_temp_path)
    
            # Convert the QR code image for tkinter
            self.display_image(img_temp_path)
    
        def display_image(self, img_path):
            image = Image.open(img_path)
            image = image.resize((350, 350), Image.ANTIALIAS)
            photo = ImageTk.PhotoImage(image)
    
            self.canvas.image = photo  # Keep a reference to avoid garbage collection
            self.canvas.create_image(175, 175, image=photo)
    
    if __name__ == "__main__":
        app = QRCodeApp()
        app.mainloop()
    

    This script provides a basic QR Code generator with a GUI. You type the text you want to encode into the Entry widget, press the "Generate QR" button, and the application displays the generated QR code on the Canvas.

    You can expand this basic application by adding more features, such as saving the QR code to a file with custom file paths, changing the QR code specifications dynamically (color, size, error correction level), etc.