用PyQt做了一個(gè)無(wú)邊框登陸窗口,效果如下: 下面是代碼:
# -*- coding: utf-8 -*- from PyQt4 import QtGui ,Qt ,QtCore
image=QtGui.QImage() bgImage=image.load("images/logins.png")
class labelBtn(QtGui.QLabel): """ 自定義圖片按鈕類(lèi) """ def __init__(self,ID): super(labelBtn, self).__init__() self.setMouseTracking(True) self.ID=ID
def mouseReleaseEvent(self,event): #注: #鼠標(biāo)點(diǎn)擊事件 self.parent().btnHandle(self.ID)
def enterEvent(self,event): #鼠標(biāo)進(jìn)入時(shí)間 self.parent().btnEnter(self.ID)
def leaveEvent(self,event): #鼠標(biāo)離開(kāi)事件 self.parent().btnLeave(self.ID)
class login(QtGui.QMainWindow): def __init__(self,parent=None): super(login, self).__init__(parent) self.setWindowTitle(u"學(xué)生體能健康測(cè)試軟件") self.setFixedSize(347,264) self.setWindowIcon(QtGui.QIcon("images/umbrella.png")) #窗口居中顯示 desktop =QtGui.QApplication.desktop() width = desktop.width() height = desktop.height() self.move((width - self.width())/2, (height - self.height())/2) self.setMouseTracking(True) #無(wú)邊框 self.setWindowFlags(Qt.Qt.FramelessWindowHint) #顯示托盤(pán)信息 self.trayIcon = QtGui.QSystemTrayIcon(self) self.trayIcon.setIcon(QtGui.QIcon("images/umbrella.png")) self.trayIcon.show() self.trayIcon.activated.connect(self.trayClick) #點(diǎn)擊托盤(pán) self.trayMenu() #右鍵菜單
label_user = QtGui.QLabel(u"賬號(hào)",self) label_user.setGeometry(QtCore.QRect(125, 135, 50, 22)) label_passwd = QtGui.QLabel(u"密碼",self) label_passwd.setGeometry(QtCore.QRect(125, 170, 50, 22))
self.label = QtGui.QLabel(self) self.label.setGeometry(QtCore.QRect(19, 129, 80, 80)) self.label.setPixmap(QtGui.QPixmap("images/teacher.png"))
self.lineEdit_user = QtGui.QLineEdit(u"root",self) self.lineEdit_user.setGeometry(QtCore.QRect(160, 135, 150, 22))
self.lineEdit_passwd = QtGui.QLineEdit(u'1234',self) self.lineEdit_passwd.setGeometry(QtCore.QRect(160, 170, 150, 22)) self.lineEdit_passwd.setEchoMode(QtGui.QLineEdit.Password) self.lineEdit_passwd.setValidator(QtGui.QRegExpValidator(Qt.QRegExp("[A-Za-z0-9]+"),self)) #這里也可以設(shè)置QLineEdit背景為透明 self.pushButton_login = QtGui.QPushButton(QtGui.QIcon("images/login.png"),u"登錄",self) self.pushButton_login.setGeometry(QtCore.QRect(250, 235, 75, 22))
self.pushButton_change = QtGui.QPushButton(QtGui.QIcon("images/onetwo.png"),u"身份切換",self) self.pushButton_change.setGeometry(QtCore.QRect(10, 235, 75, 22)) self.pushButton_change.setFlat(True) self.pushButton_change.setContextMenuPolicy(Qt.Qt.CustomContextMenu)
self.btn_min=labelBtn(1) #定義最小化按鈕 ID:1 self.btn_min.setParent(self) self.btn_min.setGeometry(281,0,27,23) self.btn_min.setToolTip(u"最小化")
self.btn_close=labelBtn(2) #定義關(guān)閉按鈕 ID:2 self.btn_close.setParent(self) self.btn_close.setGeometry(310,0,38,21) self.btn_close.setToolTip(u"關(guān)閉")
self.connect(self.pushButton_change, QtCore.SIGNAL("clicked()"),self.contextMenu) self.connect(self.pushButton_login, QtCore.SIGNAL("clicked()"),self.log_in)
def contextMenu(self):
def root(self): self.pushButton_change.setText(u"老師") self.label.setPixmap(QtGui.QPixmap("images/teacher.png"))
def user(self): self.pushButton_change.setText(u"學(xué)生") self.label.setPixmap(QtGui.QPixmap("images/students.png"))
def log_in(self): dlg=QtGui.QMessageBox(self) dlg.information(self, u"提示",u"應(yīng)該跳向另一個(gè)窗口,這里沒(méi)有寫(xiě)!",QtGui.QMessageBox.Ok)
def btnHandle(self,ID): #最小化 if ID==1: self.hide() self.showMinimized() elif ID==2: #關(guān)閉 self.trayIcon.hide() self.close()
def btnEnter(self,ID): #鼠標(biāo)進(jìn)入 if ID == 1: self.btn_min.setPixmap(QtGui.QPixmap("images/min.png")) elif ID == 2: self.btn_close.setPixmap(QtGui.QPixmap("images/close.png"))
def btnLeave(self,ID): #鼠標(biāo)離開(kāi) '''false.png這張圖片是不存在的,目的是要在鼠標(biāo) 離開(kāi)后還原背景,因?yàn)槟J(rèn)按鈕我已經(jīng)PS在背景上了''' self.btn_min.setPixmap(QtGui.QPixmap("images/false.png")) self.btn_close.setPixmap(QtGui.QPixmap("images/false.png"))
def trayClick(self,reason): #雙擊托盤(pán) if reason==QtGui.QSystemTrayIcon.DoubleClick: self.showNormal() else: pass
def trayMenu(self): #右擊托盤(pán)彈出的菜單 img_main = QtGui.QIcon("images/main.png") img_exit = QtGui.QIcon("images/exit.png") self.trayIcon.setToolTip(u"學(xué)生體能健康測(cè)試軟件") self.restoreAction = QtGui.QAction(img_main,u"打開(kāi)主窗口", self) self.restoreAction.triggered.connect(self.showNormal) self.quitAction = QtGui.QAction(img_exit,u"退出", self) self.quitAction.triggered.connect(QtGui.qApp.quit) self.trayIconMenu = QtGui.QMenu(self) self.trayIconMenu.addAction(self.restoreAction) self.trayIconMenu.addSeparator() self.trayIconMenu.addAction(self.quitAction) self.trayIcon.setContextMenu(self.trayIconMenu)
def resizeEvent(self,event): #重繪窗體背景 pal=QtGui.QPalette() pal.setBrush(QtGui.QPalette.Window,QtGui.QBrush(image.scaled(event.size(), Qt.Qt.KeepAspectRatioByExpanding,Qt.Qt.SmoothTransformation))) self.setPalette(pal)
"""下面這兩個(gè)才是重點(diǎn),是動(dòng)得關(guān)鍵""" def mousePressEvent(self,event): #鼠標(biāo)點(diǎn)擊事件 if event.button() == QtCore.Qt.LeftButton: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft() event.accept()
def mouseMoveEvent(self,event): #鼠標(biāo)移動(dòng)事件 if event.buttons() ==QtCore.Qt.LeftButton: self.move(event.globalPos() - self.dragPosition) event.accept()
if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) frm = login() frm.show() sys.exit(app.exec_())
注 :目前的問(wèn)題是QLabel相應(yīng)的是點(diǎn)擊事件,不是單擊事件,這個(gè)問(wèn)題還沒(méi)用解決! |
|