【问题标题】:AttributeError: 'StringVar' object has no attribute 'encode'AttributeError: 'StringVar' 对象没有属性 'encode'
【发布时间】:2019-07-14 15:07:24
【问题描述】:

我正在编写一个程序来根据提供的消息和密码生成一个加密的 qr,但它一直返回相同的错误。

我尝试将值传递给其他变量

main.py

from tkinter import *
import crypt

root=Tk()
root.title("QR Crypt")
root.geometry("800x600+0+0")

heading = Label(root, text="QR Code Encrypt", font=("arial",54,"bold"), fg="steelblue").pack()
label1 = Label(root, text="MESSAGE: ", font=("arial",24,"bold"), fg="black").place(x=10,y=200)
label2 = Label(root, text="PASSWORD: ", font=("arial",24,"bold"), fg="black").place(x=10,y=300)
message = StringVar()
entry_box = Entry(root, textvariable=message, width=80, bg="lightgreen").place(x=280, y=210)
passwd = StringVar()
entry_box = Entry(root, textvariable=passwd, width=30, bg="lightgreen").place(x=280, y=310)

def generate_qr():
    crypt.crypt_f(passwd,message)
    canvas = Canvas(root, width = 300, height = 300)
    canvas.pack()
    img = PhotoImage(file="myqr.svg")
    canvas.create_image(20,20, anchor=NW, image=img)

work = Button(root, text="Generate", width=30, height=5, bg="lightblue", command=generate_qr).place(x=250,y= 400)

mainloop()

crypt.py

import base64
import os
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.fernet import Fernet
import qrcode

def crypt_f(password_provided,message_rcvd):
    password = password_provided.encode()
    salt = b'\x8f\x9f3\xf1V\\\n:\xa5\x87h\x9e*\xd1\xc4\xda\xa9.\x96\xfc/\xa9\xb4\x0e\xc8wD\x9d\xee\xeb\xb1E'
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=default_backend()
)
    key = base64.urlsafe_b64encode(kdf.derive(password))

    f = Fernet(key)
    encrypted = f.encrypt(message_rcvd)
    qrcode.qr_ready(encrypted)

qrcode.py

from pyqrcode import *

def qr_ready(qr_rcvd):
    toqr=qr_rcvd
    qrcode = create(toqr)
    qrcode.svg("myqr.svg",scale=10)

我希望它在另一个画布中返回二维码,但它返回错误:

AttributeError: 'StringVar' 对象没有属性 'encode'

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    StringVar 是一个自定义类,而不是字符串。使用 get 方法将其值作为字符串检索,例如:

    crypt.crypt_f(passwd.get(), message.get())
    

    【讨论】:

    • 谢谢,我完全忘记了使用 get() 并以字节为单位转换消息..
    • 您对如何显示生成的 qr 图像有任何建议,因为它显示为空白,但是它确实在不同的 python 文件中显示图像
    • 尝试使用 xbm,如下例所示:pythonhosted.org/PyQRCode/rendering.html#xbm-rendering
    • 仍然是同样的问题,它只是显示相同的白框,没有别的
    • 啊,我忘了,你应该在 Tkinter 中对图像进行全局引用,否则垃圾收集器会清理它;做canvas.img = PhotoImage(file="myqr.svg")(丑陋的f***)或将定义img = PhotoImage()移动到entry_box赋值下面,然后在你的函数中设置实际图像,afaik,img.config(file="myqr.svg")
    【解决方案2】:

    main.py

    from tkinter import *
    import crypt
    from PIL import ImageTk,Image
    import time
    
    root=Tk()
    root.title("QR Crypt")
    root.geometry("800x600+0+0")
    
    heading = Label(root, text="QR Code Encrypt", font=("arial",54,"bold"), fg="steelblue").pack()
    label1 = Label(root, text="MESSAGE: ", font=("arial",24,"bold"), fg="black").place(x=10,y=200)
    label2 = Label(root, text="PASSWORD: ", font=("arial",24,"bold"), fg="black").place(x=10,y=300)
    message = StringVar()
    passwd = StringVar()
    entry_box1 = Entry(root, textvariable=message, width=80, bg="lightgreen").place(x=280, y=210)
    entry_box2 = Entry(root, textvariable=passwd, width=30, bg="lightgreen").place(x=280, y=310)
    
    def generate_qr():
        crypt.crypt_f(passwd.get(), message.get())
        time.sleep(2)
        canvas = Canvas(root, width = 650, height = 650)
        canvas.pack()
        img = ImageTk.PhotoImage(Image.open("myqr.png"))
        canvas.create_image(20,20, anchor=NW, image=img)
    
    
    work = Button(root, text="Generate", width=30, height=5, bg="lightblue", command=generate_qr).place(x=250,y= 400)
    
    mainloop()
    

    qrcode.py

    from pyqrcode import *
    
    def qr_ready(qr_rcvd):
        toqr=qr_rcvd
        qrcode = create(toqr)
        qrcode.png("myqr.png",scale=10)
    

    crypt.py

    import base64
    import os
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
    from cryptography.fernet import Fernet
    import qrcode
    
    def crypt_f(password_provided,message_rcvd):
        password = password_provided.encode()
        messge = message_rcvd.encode()
        salt = b'\x8f\x9f3\xf1V\\\n:\xa5\x87h\x9e*\xd1\xc4\xda\xa9.\x96\xfc/\xa9\xb4\x0e\xc8wD\x9d\xee\xeb\xb1E'
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=salt,
            iterations=100000,
            backend=default_backend()
    )
        key = base64.urlsafe_b64encode(kdf.derive(password))
    
        f = Fernet(key)
        encrypted = f.encrypt(messge)
        qrcode.qr_ready(encrypted)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2021-04-23
      • 2016-11-18
      • 2015-12-30
      • 2019-04-21
      • 2020-07-13
      相关资源
      最近更新 更多