前言
为了保护自己的工作成果,控制在合理的范围内使用,设计一个用于Token验证的弹窗.
代码
class TokenDialog(QDialog):def __init__(self, parent=None, login_user=None, mac_addr=None, func=None):super(TokenDialog, self).__init__(parent)self.login_user = login_userself.mac_addr = mac_addrself.func = funcself.init_ui(parent)self.setWindowTitle("TOKEN验证窗口")self.setWindowFlags(Qt.FramelessWindowHint) # 设置窗口无边框self.setFixedSize(400, 200)def init_ui(self, parent):'''水平布局'''self.msg_res = QLabel("")hbox = QHBoxLayout()self.save_btn = QPushButton()self.save_btn.setText('验证')self.save_btn.clicked.connect(lambda: self.save_btn_click())self.cancel_btn = QPushButton()self.cancel_btn.setText('取消')self.cancel_btn.clicked.connect(self.cancel_btn_click)hbox.addWidget(self.save_btn)hbox.addWidget(self.cancel_btn)fbox = QFormLayout()self.email_text = QLineEdit()self.email_text.setPlaceholderText("用户名")self.email_text.setText(self.login_user)self.mac_text = QLineEdit()self.mac_text.setPlaceholderText("mac地址")self.mac_text.setText(self.mac_addr)self.pw_text = QLineEdit()self.pw_text.setPlaceholderText("Token不能为空.")self.pw_text.setEchoMode(QLineEdit.Password)if os.path.exists(TOKEN_PATH):token_v = get_json_data(TOKEN_PATH)self.pw_text.setText(token_v)# self.pw_text.setText("OTA123456..")fbox.addRow(QLabel("用户名:"), self.email_text)fbox.addRow(QLabel('MAC:'), self.mac_text)fbox.addRow(QLabel('Token:'), self.pw_text)fbox.addRow(self.msg_res)vbox = QVBoxLayout()vbox.addLayout(fbox)vbox.addLayout(hbox)self.setLayout(vbox)def get_val(self):return {"user": self.email_text.text().strip(),"token": self.pw_text.text().strip()}def save_btn_click(self):ret_dict = self.get_val()if self.func and ret_dict["token"]:self.func(ret_dict["token"])def cancel_btn_click(self):self.close()def get_add_dialog(parent=None, login_user=None, mac_addr=None, func=None):dialog = TokenDialog(parent, login_user, mac_addr, func)dialog.setFixedSize(400, 200)return dialog.exec()if __name__ == '__main__':app = QApplication(sys.argv)example = TokenDialog()example.show()sys.exit(app.exec_())