首先,需要在QT控件属性中勾选checkable、autoExclusive。如下图所示:
设置后,ui文件所转化的py文件里就会有以下两句话 :
self.pushButton.setCheckable(True)
self.pushButton.setAutoExclusive(True)
然后在QT designer中右键按钮,改变样式表,写入以下代码
QPushButton{
text-decoration:none;
background:#05B8CC;
color:#f2f2f2;
padding: 10px 30px 10px 30px;
font-size:16px;
font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif;
font-weight:bold;
border-radius:3px;
}
QPushButton::checked,QToolButton::checked{ background: #3C79F2; border-color: #11505C; font-weight: bold; font-family:Microsoft YaHei; }
或在py文件中直接修改
self.pushButton_2.setStyleSheet(_fromUtf8("QPushButton{\n"
" text-decoration:none; \n"
" background:#05B8CC;\n"
" color:#f2f2f2; \n"
" \n"
" padding: 10px 30px 10px 30px; \n"
" font-size:16px; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
" font-weight:bold; \n"
" border-radius:3px; \n"
"}\n"
"QPushButton:checked{\n"
"background: #3C79F2;\n"
"border-color: #11505C;\n"
"font-weight: bold;\n"
"font-family:Microsoft YaHei;\n"
"}"
))
QPushButton中为原来样式、QPushuButton::checked为点击按钮后改变的样式
点击按钮前按钮样式如下图所示,
点击按钮后样式:
鼠标移至按钮,按钮透明度改变,点击按钮时透明度恢复并且文字位置会发生改变。
设置方法同样式1,先在代码中加上以下内容:
self.pushButton.setCheckable(True)
self.pushButton.setAutoExclusive(True)
并为按钮添加相应样式:
self.pushButton_2.setStyleSheet(_fromUtf8("QPushButton{\n"
"font-size:26px; \n"
"color:#f2f2f2; \n"
"font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37,255);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
"QPushButton:hover{\n"
" font-size:26px; \n"
"color:#f2f2f2; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37, 180);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
"QPushButton:pressed{\n"
" font-size:26px; \n"
"color:#f2f2f2; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37, 255);\n"
"border:2px solid white;border-radius:10px;\n"
"padding-left:3px;\n"
"padding-top:3px;\n"
"}"
))
QPushButton{}内表示原来样式, QPushButton:hover{}表示鼠标悬停时样式,QPushButton:pressed{}表示鼠标按下时样式。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ipc_main.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(577, 371)
Form.setStyleSheet(_fromUtf8(""))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(200, 60, 221, 81))
font = QtGui.QFont()
font.setFamily(_fromUtf8("微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif"))
font.setPointSize(-1)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label.setFont(font)
self.label.setStyleSheet(_fromUtf8("QLabel{\n"
" text-decoration:none; \n"
" background:#2f435e; \n"
" color:#f2f2f2; \n"
" \n"
" padding: 10px 30px 10px 30px; \n"
" font-size:26px; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
" font-weight:bold; \n"
" border-radius:3px; \n"
"}"))
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName(_fromUtf8("label"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(70, 190, 161, 81))
self.pushButton.setStyleSheet(_fromUtf8("QPushButton{\n"
"font-size:26px; \n"
"color:#f2f2f2; \n"
"font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(0,33,99, 255);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
" \n"
"QPushButton:hover{ \n"
"font-size:26px; \n"
"color:#f2f2f2; \n"
"font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(0,33,99, 150);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
" \n"
"QPushButton:pressed{\n"
"font-size:26px; \n"
"color:#f2f2f2; \n"
"font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(0,33,99, 255);\n"
"border:2px solid white;border-radius:10px;\n"
"padding-left:3px;\n"
"padding-top:3px;\n"
"} "))
self.pushButton.setCheckable(True)
self.pushButton.setAutoExclusive(True)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(350, 200, 161, 81))
self.pushButton_2.setStyleSheet(_fromUtf8("QPushButton{\n"
"font-size:26px; \n"
"color:#f2f2f2; \n"
"font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37,255);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
"QPushButton:hover{\n"
" font-size:26px; \n"
"color:#f2f2f2; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37, 180);\n"
"border:2px solid white;border-radius:10px;\n"
"}\n"
"QPushButton:pressed{\n"
" font-size:26px; \n"
"color:#f2f2f2; \n"
" font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; \n"
"background-color: rgba(95, 255, 37, 255);\n"
"border:2px solid white;border-radius:10px;\n"
"padding-left:3px;\n"
"padding-top:3px;\n"
"}"
))
self.pushButton_2.setCheckable(True)
self.pushButton_2.setAutoExclusive(True)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "ipc升级工具", None))
self.pushButton.setText(_translate("Form", "内核升级", None))
self.pushButton_2.setText(_translate("Form", "TFTP升级", None))
import picture_rc,sys
class MainWindow(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_Form()# Ui_Dialog为.ui产生.py文件中窗体类名,经测试类名以Ui_为前缀,加上UI窗体对象名(此处为Dialog,见上图)
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #构造函数中设置去掉菜单栏
def mousePressEvent(self, event):
self.pressX = event.x() #记录鼠标按下的时候的坐标
self.pressY = event.y()
def mouseMoveEvent(self, event):
x = event.x()
y = event.y() #获取移动后的坐标
moveX = x-self.pressX
moveY = y-self.pressY #计算移动了多少
positionX = self.frameGeometry().x() + moveX
positionY = self.frameGeometry().y() + moveY #计算移动后主窗口在桌面的位置
self.move(positionX, positionY) #移动主窗口
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp=MainWindow()
myapp.show()
#myapp.btn.clicked.connect(ui2.show)
app.exec_()