import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QPushButton, QWidget, QToolTip
from PyQt5.QtGui import QIcon
# 从QMainWindow继承的类
class MainWindow(QMainWindow):
# 初始化
# parent=None 保证了MainWindow为主窗口
def __init__(self, parent=None):
# 调用父类
super(MainWindow,self).__init__(parent)
# 设置窗口名称和大小
self.setWindowTitle('Window')
self.resize(400,300)
# 添加QPushButton类控件
self.button = QPushButton('exit application')
self.button.clicked.connect(self.onClick_Button_quit)
# 给self.button添加提示信息
self.button.setToolTip('Press to exit the application')
# 使self.button显示在屏幕上
self.layout = QHBoxLayout()
self.layout.addWidget(self.button)
self.mainFrame = QWidget()
self.mainFrame.setLayout(self.layout)
# 使mainFrame充满整个窗口
self.setCentralWidget(self.mainFrame)
def onClick_Button_quit(self):
# sender()的方法:
# objectName(),获取objectName
# text(),获取text
sender = self.sender()
print(' The \'' + sender.text() + '\' key is pressed')
# 获取app单例对象
# 这里是,获取当前对象
app = QApplication.instance()
# 退出程序
app.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用程序图标
# './img/icon.ico'为.ico图片的路径
app.setWindowIcon(QIcon('./img/icon.ico'))
win = MainWindow()
win.show()
sys.exit(app.exec_())
PyQt5学习——给控件添加提示信息
最新推荐文章于 2022-07-14 11:55:46 发布