import sys
from PyQt5.QtWidgets import QHBoxLayout, QDesktopWidget, QMainWindow, QApplication, QPushButton,QWidget
from PyQt5.QtGui import QIcon
class QuitApplication(QMainWindow): # 这里继承QMainWindow类,用该类中的方法
def __init__(self):
super(QuitApplication,self).__init__()
self.resize(300,120)
self.setWindowTitle('退出应用程序许')
# 添加button
self.button1 = QPushButton('退出应用程序')
# 将信号与槽关联
self.button1.clicked.connect(self.onClick_Button)
layout = QHBoxLayout()
layout.addWidget(self.button1)
mainFrame = QWidget()
mainFrame.setLayout(layout)
self.setCentralWidget(mainFrame)
# 按钮单机事件的方法(自定义的槽)
def onClick_Button(self):
sender = self.sender() # 获取发送消息的按钮
print(sender.text()+'按钮被按下')
app = QApplication.instance()
app.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QuitApplication()
main.show()
sys.exit(app.exec_())
综合案例
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
class QLineEditeDemo(QWidget):
def __init__(self):
super(QLineEditeDemo, self).__init__()
self.initUI()
def initUI(self):
edit1 = QLineEdit() # 创建编辑控件1
# 使用int校验器
edit1.setValidator(QIntValidator())
edit1.setMaxLength(4)
edit1.setAlignment(Qt.AlignRight)
edit1.setFont(QFont('Arial', 20))
# 使用浮点型的校验器
edit2 = QLineEdit() # 创建控件edit2
edit2.setValidator(QDoubleValidator(0.99,99.99,2))
# 使用掩码校验器
edit3 = QLineEdit()
edit3.setInputMask('99_999_99999;#')
edit4 = QLineEdit()
edit4.textChanged.connect(self.textChange)
edit5 =QLineEdit()
edit5.setEchoMode(QLineEdit.Password)
edit5.editingFinished.connect(self.enterPress)
edit6 = QLineEdit()
edit6.setReadOnly(True)
# 创建表单布局
formLayout = QFormLayout()
formLayout.addRow('整数校验', edit1) # 将edite1控件放到表单布局中
formLayout.addRow('浮点型校验',edit2) # 将edite1控件放到表单布局中
formLayout.addRow('Input Mask',edit3)
formLayout.addRow('w文本变化',edit4)
formLayout.addRow('密码',edit5)
formLayout.addRow('只读',edit6)
self.setLayout(formLayout)
self.setWindowTitle('QLineEdite综合案例')
def textChange(self,test):
print('输入的内容'+test)
def enterPress(self):
print('已输入值')
if __name__ == '__main__':
app = QApplication(sys.argv)
main1 = QLineEditeDemo()
main1.show()
sys.exit(app.exec_())