首页 > 代码库 > Python生成二维码和短网址
Python生成二维码和短网址
使用PyQt5。
方便电脑传输网址或者文字内容到手机上面,扫一下二维码就行了,短网址偶尔也能用上。
1 #!/usr/bin/env python3 2 from PyQt5.QtGui import QPixmap 3 from PyQt5 import QtWidgets 4 from PyQt5.QtCore import Qt 5 import requests 6 from os.path import expanduser 7 8 9 class Ui_qrDlg(): 10 11 def setupUi(self): 12 self.setFixedSize(650, 400) 13 self.setWindowTitle(‘生成二维码/短网址‘) 14 self.setWindowFlags(Qt.WindowCloseButtonHint) 15 self.center() 16 17 gBoxStyle = ‘‘‘QGroupBox{border-width:1px;border-style:solid;border-color:grey;margin-top:0.85ex;} 18 QGroupBox::title{subcontrol-origin:margin;subcontrol-position:top left;left:10px;margin-left:0px;padding:0 1px;}‘‘‘ 19 self.gBoxInfo = QtWidgets.QGroupBox(self, title=‘输入内容‘) 20 self.gBoxInfo.setGeometry(12, 10, 300, 380) 21 self.gBoxInfo.setStyleSheet(gBoxStyle) 22 self.textEdit = QtWidgets.QTextEdit(self.gBoxInfo) 23 self.textEdit.setGeometry(7, 30, 286, 300) 24 self.textEdit.setObjectName(‘textEdit‘) 25 self.urlShow = QtWidgets.QTextEdit(self) 26 self.urlShow.setGeometry(385, 12, 253, 30) 27 self.urlShow.setObjectName(‘urlShow‘) 28 self.urlShow.setReadOnly(True) 29 self._label = QtWidgets.QLabel(self, text=‘短网址:‘) 30 self._label.setGeometry(325, 12, 60, 30) 31 self._label.setAlignment(Qt.AlignCenter) 32 self.btnCreate = QtWidgets.QPushButton(self.gBoxInfo, text=‘生成‘) 33 self.btnCreate.setGeometry(7, 338, 286, 35) 34 self.btnCreate.setObjectName(‘btnCreate‘) 35 self.gBoxPic = QtWidgets.QGroupBox(self, title=‘二维码‘) 36 self.gBoxPic.setGeometry(325, 45, 313, 345) 37 self.gBoxPic.setStyleSheet(gBoxStyle) 38 self.label = QtWidgets.QLabel(self.gBoxPic) 39 self.label.setGeometry(0, 14, 313, 313) 40 self.label.setAlignment(Qt.AlignCenter) 41 self.btnSave = QtWidgets.QPushButton(self.gBoxPic, text=‘另存为‘) 42 self.btnSave.setGeometry(220, 300, 80, 40) 43 self.btnSave.setObjectName(‘btnSave‘) 44 self.btnSave.hide() 45 46 def center(self): 47 screen = QtWidgets.QDesktopWidget().screenGeometry() 48 size = self.geometry() 49 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) 50 51 52 class qrDlg(QtWidgets.QDialog, Ui_qrDlg): 53 54 def __init__(self, parent=None): 55 super(qrDlg, self).__init__(parent) 56 self.setupUi() 57 self.btnCreate.clicked.connect(self.on_btnCreate_clicked) 58 self.btnSave.clicked.connect(self.on_btnSave_clicked) 59 60 def on_btnCreate_clicked(self): 61 import qrcode 62 info = self.textEdit.toPlainText().strip() 63 if not info: 64 QtWidgets.QMessageBox.information(self, (‘提示‘), (‘请填写信息‘), QtWidgets.QMessageBox.Yes) 65 self.btnSave.hide() 66 self.label.clear() 67 else: 68 qr = qrcode.QRCode(version=12, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=3, border=4) 69 qr.add_data(info) 70 # qr.make(fit=True) 71 img = qr.make_image() 72 filename = expanduser(‘~‘) + ‘/.qr_code.png‘ 73 img.save(filename) 74 pic = QPixmap(filename) 75 self.label.setPixmap(pic) 76 self.btnSave.show() 77 if info.startswith(‘http://‘) or info.startswith(‘https://‘): 78 self.urlShow.setText(get_short_url(info)) 79 self.urlShow.selectAll() 80 81 def on_btnSave_clicked(self): 82 qrPic = self.label.pixmap() 83 if qrPic: 84 filename, extra = QtWidgets.QFileDialog.getSaveFileName(self, ‘另存为‘, expanduser(‘~‘) + ‘/Desktop‘, ‘PNG Images (*.png)‘) 85 if filename: 86 qrPic.save(filename, ‘png‘) 87 88 89 def get_short_url(url, data=http://www.mamicode.com/None): 90 baseurl = ‘http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=‘ 91 url = baseurl + url 92 rep = requests.get(url, timeout=60) 93 return rep.json()[0][‘url_short‘] 94 95 96 if __name__ == ‘__main__‘: 97 import sys 98 app = QtWidgets.QApplication(sys.argv) 99 Dlg = qrDlg() 100 Dlg.show() 101 sys.exit(app.exec_())
效果如下图:
Python生成二维码和短网址
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。