Python產生二維碼和短網址

來源:互聯網
上載者:User

標籤:flags   setup   const   技術分享   self   box   label   python3   rip   

使用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=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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.