pyqt5_控件_gridLayout_模拟登陆窗体
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys class Example(QWidget): _startPos = None _endPos = None _isTracking = False _Menu = None def __init__(self): super(Example, self).__init__() self._initUI() def _initUI(self): self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) # 无边框并置顶 self.setWindowIcon(QIcon('./timg.jpg')) self.resize(400, 300) # 初始化背景 self.label = QLabel(self) self.label.setGeometry(0, 0, self.width(), self.height() // 2) self.label.setAutoFillBackground(True) palete = QPalette() palete.setColor(QPalette.Window, QColor(Qt.gray)) self.label.setPalette(palete) # 登录窗口模拟 self.gridParent = QWidget(self) self.gridParent.setGeometry(110, 180, 160, 98) # self.gridParent.setStyleSheet('border: 1px solid red') self.gridLayout = QGridLayout(self.gridParent) self.gridLayout.setContentsMargins(0, 0, 0, 0) commonFont = QFont() commonFont.setPointSize(8) commonFont.setFamily('微软雅黑') userLabel = QLabel(self.gridParent) userLabel.setText('用户名:') userLabel.setFont(commonFont) self.gridLayout.addWidget(userLabel, 0, 0, 1, 1) userEdit = QLineEdit(self.gridParent) userEdit.setPlaceholderText('输入用户名') userEdit.setFont(commonFont) self.gridLayout.addWidget(userEdit, 0, 1, 1, 1) pwdLabel = QLabel(self.gridParent) pwdLabel.setText('密 码:') pwdLabel.setFont(commonFont) self.gridLayout.addWidget(pwdLabel, 1, 0, 1, 1) pwdEdit = QLineEdit(self.gridParent) pwdEdit.setPlaceholderText('输入密码') pwdEdit.setFont(commonFont) self.gridLayout.addWidget(pwdEdit, 1, 1, 1, 1) loginBtn = QPushButton(self.gridParent) loginBtn.setText('登录服务器') loginBtn.setFont(commonFont) # 设置背景色,按钮的用调色板竟然不行,只能用样式表 loginBtn.setStyleSheet('background-color: rgb(193,210,240);' 'border-radius:4px;' 'color:white;' ) loginBtn.setFixedSize(118, 22) self.gridLayout.addWidget(loginBtn, 2, 1, 1, 1) moreFuncs = QLabel(self.gridParent) moreFuncs.setText('<a href=\'http:\'>更多功能</a>') moreFuncs.setFont(commonFont) moreFuncs.setAlignment(Qt.AlignRight) moreFuncs.setContextMenuPolicy(Qt.CustomContextMenu) # 禁用菜单 moreFuncs.customContextMenuRequested.connect(self._customContextMenu) # 自定义菜单信号 self.moreFun = moreFuncs self.gridLayout.addWidget(moreFuncs, 3, 1, 1, 1) self.show() def _customContextMenu(self, pos: QPoint): if not self._Menu: self._Menu = QMenu(self) self._Menu.addAction('打开') self._Menu.addAction('保存') self._Menu.addAction('退出') self._Menu.triggered.connect(self._menu_choose_event) #选择事件 self._Menu.exec(self.moreFun.mapToGlobal(pos)) def _menu_choose_event(self, e:QAction): print(e.text()) def mouseMoveEvent(self, e: QMouseEvent): # 重写移动事件 if self._isTracking: self._endPos = e.pos() - self._startPos self.move(self.pos() + self._endPos) def mousePressEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = True self._startPos = QPoint(e.x(), e.y()) def mouseReleaseEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = False self._startPos = None self._endPos = None if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec())
# 效果图