参考视频教程链接:
https://www.bilibili.com/video/BV154411n79k?p=24&spm_id_from=pageDriver
共有三种窗口:
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon #添加图标 class FirstMainWin(QMainWindow): def __init__(self,parent=None): super(FirstMainWin,self).__init__(parent) #设置主窗口标题 self.setWindowTitle('第一个主窗口应用') #设置主窗口尺寸 self.resize(400,300) #获得状态栏 self.status = self.statusBar() self.status.showMessage('只存在五秒的消息',5000) #状态栏消息 if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('./images/xx.ico')) #显示的图标 main = FirstMainWin() main.show() sys.exit(app.exec_())
理论方法:左右/上下边距离左右/上下屏幕距离相等(整体宽度/高度-窗口宽度/高度再除以2)
import sys from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication from PyQt5.QtGui import QIcon #添加图标 class CenterForm(QMainWindow): def __init__(self,parent=None): super(CenterForm,self).__init__(parent) #设置主窗口标题 self.setWindowTitle('让窗口居中') #设置主窗口尺寸 self.resize(400,300) def center(self): #获取屏幕坐标系 screen = QDesktopWidget().screenGeometry() #获取窗口坐标系 size = self.geometry() newLeft = (screen.width()-size.width())/2 newTop = (screen.height()-size.height())/2 #进行移动 self.move(newLeft,newTop) if __name__ == '__main__': app = QApplication(sys.argv) main = CenterForm() main.show() sys.exit(app.exec_())
import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QWidget class QuitApplication(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_())