AttributeError: StringVar object has no attribute encode

2024/10/10 13:13:30

I'm making a program to generate an encrypted qr from the message and password provided, but it keeps on returning the same error.

I tried passing the value to other variables

main.py

from tkinter import *
import cryptroot=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 qrcodedef 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_rcvdqrcode = create(toqr)qrcode.svg("myqr.svg",scale=10)

I expect it to return the QR code in another canvas but it is returning the error:

AttributeError: 'StringVar' object has no attribute 'encode'

Answer

StringVar is a custom class, not string. Use get method to retrieve its value as string, like:

crypt.crypt_f(passwd.get(), message.get())
https://en.xdnf.cn/q/118450.html

Related Q&A

Reading specific column from a csv file in python

I am writing some python code to practice for an exam in January. I need to read the second column into my code and print it out. If possible i also need to add data to specific columns. The code i hav…

Date Time Series wise grouping of data and distribution

I am trying the merge the datetime series with a repository data while grouping by name and summing the values. File1.csv Timeseries,Name,count 07/03/2015 06:00:00,Paris,100 07/03/2015 06:00:00,Paris,6…

Trying to run Python in html

I am trying to run a python program in html but I am getting an error. First it says Then if I type anything it appears with this error This was the Html code <html><head><title>Antho…

Removing from a string all the characthers included between two specific characters in Python

Whats a fast way in Python to take all the characters included between two specific characters out of a string?

Pyside6: Create QTabWidget with function rather than class

Ive been trying to make an application using Pyside6 and cant seem to understand why we cant create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so,…

Pythons End Of Life

What exactly will happen to Python 2.7 after 1/2020?I understand that Python 2.7 will no longer be supported but what will actually happen? Does it mean that decision makers will delete the whole cod…

Gathering numerical data from a string input

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest…

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …