Python+Qt生成二维码

利用Qrcode生成二维码,用PIL显示图片,以Qt做界面。调用了不少第三方库,虽说可以少点,大才小用吧。

 

import qrcode
import sys
from PyQt4 import QtGui,QtCore
from PIL import Image
import time

class Window (QtGui.QWidget):
def __init__( self ) :
super(Window,self).__init__()
self.setWindowTitle(u"二维码生成器")
self.resize(300,400)
gridlayout = QtGui.QGridLayout()
self.Label=QtGui.QLabel("文本")
self.Text=QtGui.QLineEdit()
self.Make=QtGui.QPushButton("生成")
self.Exit=QtGui.QPushButton("退出")
gridlayout.addWidget( self.Label , 0, 0,1,1)
gridlayout.addWidget( self.Text , 0, 1,1,3)
gridlayout.addWidget( self.Make , 1, 3,1,1)
gridlayout.addWidget( self.Exit , 1, 2,1,1)

self.setLayout(gridlayout)

self.connect( self.Exit, QtCore.SIGNAL( 'clicked()' ), self.close )
self.connect( self.Make, QtCore.SIGNAL( 'clicked()' ),self.QrMake)

def QrMake(self):
qr = qrcode.QRCode(
version=1,
box_size=10,
border=4,
)
s=self.Text.text()
qr.add_data(s)
qr.make(fit=True)
img = qr.make_image()
img.save('Qr.png')

time.sleep(2)
i=Image.open("Qr.png")
i.show()

app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

1条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注