Вылезает ошибка после нажатия на кнопку
Почему-то выдает ошибку. Помогите пожалуйста разобраться
Ошибка:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C://pythonProject/qrgenerator.py", line 18, in QR_generator
qr = pyqrcode.create(value)
File "C:\\pythonProject\venv\lib\site-packages\pyqrcode\__init__.py", line 111, in create
return QRCode(content, error, version, mode, encoding)
File "C:\\pythonProject\venv\lib\site-packages\pyqrcode\__init__.py", line 137, in __init__
guessed_content_type, encoding = self._detect_content_type(content, encoding)
File "C:\\pythonProject\venv\lib\site-packages\pyqrcode\__init__.py", line 307, in _detect_content_type
c = content.encode('shiftjis')
AttributeError: 'StringVar' object has no attribute 'encode'
import pyqrcode
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title('QR Генератор')
root.geometry('300x200+700+400')
root["bg"] = "#c0c0c0"
value = StringVar()
ent = Entry(textvariable=value, font='17')
ent.pack()
ent.place(x=55,y=5)
def QR_generator():
qr = pyqrcode.create(value)
qr.png('qrcode.png')
butt = Button(root, text='Генерировать', font='35', bg='#ccc', width=19, command=QR_generator)
butt.pack()
butt.place(x=55, y=33)
root.mainloop()
Ответы (1 шт):
Автор решения: insolor
→ Ссылка
У вас value - это объект класса StringVar, специфичного для tkinter. Очевидно, pyqrcode ничего не знает об этом классе.
По ошибке похоже (мне лень было смотреть документацию), что pyqrcode.create() ожидает, что ему передадут просто строку (у строки есть метод encode, в отличие от StringVar), поэтому нужно передавать не сам объект value, а строку из него:
def QR_generator():
qr = pyqrcode.create(value.get()) # Метод get получает значение из объекта StringVar
qr.png('qrcode.png')