Search code examples
pythontkinterqr-code

Python Qrcode problem with input in tkinter


i am trying to create an program that asks you an link and than it gives you an qrcode for that link. but i cant use the input i ask for so it just creates an qrcode with nothing.

I need help to ask an input to the user and then use that input converted it to an qrcode i already fixed my open button i chagned the open. to show.

import qrcode 
import tkinter
from PIL import Image



main = tkinter.Tk("Link converter to QrCode ")
main.title("Link converter to Qr Code")
main.geometry("685x85")


link = tkinter.Label(
    main, 
    width="30",
    text=('Link:'))
link.grid(row=0)


e1 = tkinter.Entry(
    main, 
    bd="5", 
    width="75",
    text=(""))
e1.grid(row=0, column=1)

qrcode.make(e1.get())
img = qrcode.make(e1.get())

button_create = tkinter.Button( 
    main, 
    text="Click to create the Qrcode.",  
    command = lambda:qrcode.make(e1.get()) and img.save("")  )
button_create.grid(row=2, column=1)




button_open = tkinter.Button ( 
    main,  
    text="Click here to open the Qrcode", 
    command = lambda: img.show(""), 
    width="25")
button_open.grid(row=2, column=0)


exit_button = tkinter.Button(
    main,
    width=("15"),
    text='Exit',
    command= lambda: main.quit())
exit_button.grid(row=4, column=0)



main.mainloop()

Solution

  • when you write this line:

    img = qrcode.make(e1.get())
    

    it creates an empty qr and when you call img, it returns this empty qr. you must do the img inside a function and call it with the buttons. Here is the solution:

    import qrcode 
    import tkinter
    from PIL import Image
    
    
    
    main = tkinter.Tk("Link converter to QrCode ")
    main.title("Link converter to Qr Code")
    main.geometry("685x85")
    
    
    link = tkinter.Label(
        main, 
        width="30",
        text=('Link:'))
    link.grid(row=0)
    
    
    e1 = tkinter.Entry(
        main, 
        bd="5", 
        width="75",
        text=(""))
    e1.grid(row=0, column=1)
    
    def makeqr():
        img = qrcode.make(e1.get())
        return img
    
    button_create = tkinter.Button( 
        main, 
        text="Click to create the Qrcode.",  
        command = lambda:makeqr().save("")  )
    button_create.grid(row=2, column=1)
    
    
    
    
    button_open = tkinter.Button ( 
        main,  
        text="Click here to open the Qrcode", 
        command = lambda: makeqr().show(""), 
        width="25")
    button_open.grid(row=2, column=0)
    
    
    exit_button = tkinter.Button(
        main,
        width=("15"),
        text='Exit',
        command= lambda: main.quit())
    exit_button.grid(row=4, column=0)
    
    
    
    main.mainloop()