一、准备工作
1、Python 3.7 (没升级)
2、pyqt5(6的api改了好多并且网上资料少)
3、pycharm社区版
4、OS widows11
二、真相
三、实际代码
主要就是个布局,界面是固定的因此采用绝对定位。
import sys from PyQt5.QtCore import Qt from PyQt5.QtGui import QIcon, QImage, QPixmap, QFont, QPalette from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QLabel, QLineEdit, QCheckBox, QPushButton, \ QMessageBox from framework.base.BaseWidget import BaseWidget class LoginViews(BaseWidget): def __init__(self): BaseWidget.__init__(self) self.initUI() def initUI(self): self.resize(360, 500) self.setFixedSize(400, 500) self.center() self.setWindowFlags(Qt.WindowCloseButtonHint|Qt.WindowStaysOnTopHint) self.setWindowTitle('亚丁号小助手登陆') self.setWindowIcon(QIcon('./framework/resource/icon/hunter.ico')) palette = QPalette() palette.setColor(QPalette.Background, Qt.white) self.setPalette(palette) pixmap = QPixmap("./framework/resource/images/hunter.png") label = QLabel(self) label.setPixmap(pixmap) label.move(60,40) custom_font = QFont() custom_font.setPointSize(18) #QApplication.setFont(custom_font, "QLabel") lbl1 = QLabel('亚丁号小助手登陆', self) lbl1.setFont(custom_font) lbl1.move(110, 44) # redImg = QImage() # QImage.load(redImg, './data/red.png', format='png') # lbl1.setPixmap(QtGui.QPixmap(redImg)) pixmap = QPixmap("./framework/resource/images/splitline.png") label = QLabel(self) label.setPixmap(pixmap) label.move(0, 90) lb_uname = QLabel('请输入用户名:', self) lb_uname.move(20, 120) self.userName=QLineEdit(self) self.userName.move(20, 150) self.userName.setPlaceholderText("请输入用户名") self.userName.setAlignment(Qt.AlignLeft) self.userName.setStyleSheet("border-width:1px;border-style:solid;width:350px;height:25px") self.userName.setText("zhangy") lb_pword = QLabel('请输入密码:', self) lb_pword.move(20, 190) self.passWord = QLineEdit(self) self.passWord.move(20, 220) self.passWord.setPlaceholderText("请输入密码") self.passWord.setAlignment(Qt.AlignLeft) self.passWord.setStyleSheet("border-width:1px;border-style:solid;width:350px;height:25px") self.passWord.setText("123123") self.passWord.setEchoMode(QLineEdit.Password) remeber_me = QCheckBox(self) remeber_me.move(20, 270) remeber_me.setText('自动登录') login_button = QPushButton(self) login_button.setText("立即登录") # text login_button.move(20, 300) login_button.clicked.connect(self.login) login_button.setStyleSheet("border-width:1px;border-style:solid;width:360px;height:28px;background-color:#0081FF;font-size:18px;color:white") lb_pword = QLabel('还没有账号?', self) lb_pword.move(102, 345) register_button = QPushButton(self) register_button.setText("立即注册") # text register_button.move(185, 342) register_button.setStyleSheet("border:none;margin-top:2px;color:#FFA500") register_button.clicked.connect(self.reg) yadinghao_button = QPushButton(self) yadinghao_button.setText("亚丁号") # text yadinghao_button.move(255, 342) yadinghao_button.setStyleSheet("border:none;margin-top:2px;color:#FFA500") yadinghao_button.clicked.connect(self.open_yadinghao) def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def reg(self): # self.window.quit() # self.window.destroy() # from framework.views.RegisterView import RegisterView # RegisterView() pass def open_yadinghao(self): import webbrowser webbrowser.open("https://www.yadinghao.com/") def login(self): errMessage = "" if len(self.userName.text()) == 0: errMessage = errMessage + "用户名不能为空!\r" if len(self.passWord.text()) == 0: errMessage = errMessage + "密码不能为空!" if errMessage != "": QMessageBox.information(self, '提示', errMessage) return user_name = self.userName.text() pass_word = self.passWord.text() from framework.views.MainViews import MainViews self.main = MainViews() self.main.show() self.hide()
四、总结
勤学习勤记录勤分享
最后送大家一首诗:
山高路远坑深,
大军纵横驰奔,
谁敢横刀立马?
惟有点赞加关注大军。