简体   繁体   English

在Tkinter GUI Python中显示QRCode

[英]QRCode displaying in tkinter GUI python

I am trying to display a QR Code in a tkinter GUI, however when I execute this code: 我正在尝试在tkinter GUI中显示QR代码,但是当我执行此代码时:

import tkinter as tk
from PIL import Image,ImageTk
import pyqrcode
from tkinter.font import Font
import random


root=tk.Tk()
root.title("QR Lottery")
root.config(bg="white")

# Defining Fonts
TitleFont = Font(family="HEX:gon Staggered 2", size="48")

def generateQR():
    num=random.randint(1,2)
    if num==1:
        QRCode=pyqrcode.create("You Win!")
        QRCode.png("QRCode.png",scale=8)
        img = Image.open('QRCode.png')
        QRCodeImg = ImageTk.PhotoImage(img)
        QRCodeLabel=tk.Label(image=QRCodeImg)
        QRCodeLabel.grid(row=2,column=1)
    else:
        QRCode=pyqrcode.create("You Lose!")
        QRCode.png("QRCode.png",scale=8)
        img = Image.open('QRCode.png')
        QRCodeImg = ImageTk.PhotoImage(img)
        QRCodeLabel=tk.Label(image=QRCodeImg)
        QRCodeLabel.grid(row=2,column=1)

#Labels
TitleLabel=tk.Label(text="qr lottery",bg="white",font=TitleFont)
TitleLabel.grid(row=1,column=1,columnspan=5)
ButtonQR=tk.Button(text="Generate!",bg="white",command=generateQR)
ButtonQR.grid(row=3,column=1)

root.mainloop()

The Image Label produced is a blank square. 生成的图像标签为空白方块。 I am unsure of why this is, as I left the background color blank. 我不确定为什么会这样,因为我将背景色留为空白。

Question : The Image Label produced is a blank square. 问题 :生成的图像标签为空白方块。 I am unsure of why this is 我不确定这是为什么

A :You must keep a reference to the image object in your Python program, by attaching it to another object. :您必须通过将图像对象附加到另一个对象来保留对图像对象的引用。

Use the following: 使用以下内容:


  1. Define your own widget QRCodeLabel by inherit from tk.Label . 定义你自己的小工具QRCodeLabel通过继承tk.Label
    Init only with parameter parent 仅使用参数parent初始化

     class QRCodeLabel(tk.Label): def __init__(self, parent, qr_data): super().__init__(parent) print('QRCodeLabel("{}")'.format(qr_data)) 
  2. Create your QRCode with the passed qr_data and save as PNG file. 使用传递的qr_data创建QRCode并另存为PNG文件。

      qrcode = pyqrcode.create(qr_data) tmp_png_file = "QRCode.png" qrcode.png(tmp_png_file, scale=8) 
  3. Create a image object from the PNG file. PNG文件创建图像对象。
    Tkinter can handle PNG image files by its own, no PIL needed. Tkinter可以自己处理PNG图像文件,不需要PIL

    NOTE : You have to use self.image to prevent garbage collection! 注意 :您必须使用self.image来防止垃圾收集!

      self.image = tk.PhotoImage(file=tmp_png_file) 
  4. Configure this Label with the self.image 使用self.image配置此Label

      self.configure(image=self.image) 

Usage : 用法

 class App(tk.Tk): def __init__(self): super().__init__() buttonQR = tk.Button(text="Generate!", bg="white", command=self.generateQR) buttonQR.grid(row=2, column=0) self.qr_label = None def generateQR(self): if self.qr_label: self.qr_label.destroy() self.qr_label = QRCodeLabel(self, random.choice(["You Win!", "You Lose!"])) self.qr_label.grid(row=1, column=0) if __name__ == "__main__": App().mainloop() 

Tested with Python: 3.5 使用Python测试:3.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM