import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton, QWidget
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QDesktopWidget
from PyQt5.QtGui import QIcon
class FirstMainWin(QMainWindow):
def __init__(self):
super(FirstMainWin, self).__init__()
self.setWindowTitle('第一个窗口应用')
self.resize(400, 300)
self.status = self.statusBar()
self.status.showMessage('只存在5秒', 5000)
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 center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
newLeft = (screen.width() - size.width()) / 2
newTop = (screen.height() - size.height()) / 2
self.move(int(newLeft), int(newTop))
def onClick_Button(self):
sender = self.sender()
print(sender.text() + '按钮被按下')
app = QApplication.instance()
app.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('./音乐.ico'))
main = FirstMainWin()
main.center()
main.show()
sys.exit(app.exec_())