Search code examples
pythontkinterqr-code

How can I add control character in QR in Python QRcode module


I've tried to add control character (e.g. tab, enter etc) between text in python QRcode module. I can't find reference or introduction how can I add it. Could you please advise it?

I just make code simply like below.

from tkinter import *
import qrcode

root = Tk()

root.title('My QRcode Generator') #window title
root.geometry("640x480+300+100") #width X hegith + x + Y

e = Entry(root, width=30)
e.pack()

label1 = Label(root, text='QRIMAGE')
label1.pack()

def btncmd():
    img = qrcode.make(e.get())
    a = e.get()
    img.save( a + '.png')
    global photo
    photo = PhotoImage(file=a+'.png')
    label1.config(image=photo)

btn1= Button(root, text='generate QRcode', command = btncmd)
btn1.pack()

root.mainloop()

Solution

  • An entry widget only accepts a single line of text. It's possible to insert a newline, but you won't be able to see it. If you want to be able to see it, you'll have to use a Text widget.

    The default behavior of a tab is to move the focus to the next widget so that the user can navigate the UI from the keyboard.

    If you want the user to be able insert a tab or newline into the entry widget, you'll have to add special bindings that insert the string and then prevent the default action.

    For example:

    def insert_special(char):
        e.insert("insert", char)
        return "break"
    
    entry.bind("<Tab>", lambda event: insert_special("\t"))
    entry.bind("<Return>", lambda event: insert_special("\n"))
    

    Note: if you use the Text widget you won't have to do the binding since it allows tabs and newlines.