python点击关闭窗口 python 按钮点击关闭窗口的实现
放大的EZ 人气:01、简介
1)使用 python 创建主窗口
2)设置标题栏图标,文字
3)设置状态栏图标文字
4)将窗口移动到屏幕中间
2、功能实现
1) QtDesign 实现
就是 先画 ui 然后转python
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'untitled.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) # 设置窗口大小 self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 100, 60)) #设置 按钮位置大小 self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.onButtonClick) #绑定按键点击事件 self.verticalLayout.addWidget(self.pushButton) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "关闭窗口")) # 设置按钮文字 def onButtonClick(self): # sender 是发送信号的对象,此处发送信号的对象是button1按钮 sender = self.sender() print(sender.text() + ' 被按下了') qApp = QApplication.instance() qApp.quit() # 关闭窗口 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
可以设置 按钮位置和大小,但是速度有点慢
2)使用 python 直接写出来
速度快一些,但是还没有掌握怎么设置按钮大小和位置··
# -*- coding: utf-8 -*- ''' 【简介】 PyQT5中主窗口例子 ''' import sys from PyQt5.QtWidgets import QMainWindow , QApplication, QDesktopWidget,QPushButton,QHBoxLayout,QWidget from PyQt5.QtGui import QIcon class MainWidget(QMainWindow): def __init__(self,parent=None): super(MainWidget,self).__init__(parent) # 设置主窗体标签 self.setWindowTitle("QMainWindow 例子") #设置主窗口文字 self.resize(400, 200) #设置主窗口大小 self.status = self.statusBar() #获取主窗口状态栏 self.status.showMessage("这是状态栏提示",5000) #设置主窗口状态栏文字 self.center() # 调用将窗口设置中间函数 self.button1 = QPushButton('关闭主窗口') #实例化一个 按键 self.button1.setStyleSheet("background-color: red") # 设置按钮的风格和颜色 self.button1.clicked.connect(self.onButtonClick) #绑定按键点击事件 layout = QHBoxLayout() layout.addWidget(self.button1) main_frame = QWidget() main_frame.setLayout(layout) self.setCentralWidget(main_frame) def onButtonClick(self ): #sender 是发送信号的对象,此处发送信号的对象是button1按钮 sender = self.sender() print( sender.text() + ' 被按下了' ) qApp = QApplication.instance() qApp.quit() def center(self): screen = QDesktopWidget().screenGeometry() # 获取当前屏幕对象 size = self.geometry() # 获取窗口尺寸数据 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) # 根据屏幕尺寸 和 窗口尺寸计算 相应位置 if __name__ == "__main__": app = QApplication(sys.argv) app.setWindowIcon(QIcon("./images/cartoon1.ico")) #设置主窗口的图标 main = MainWidget() #获取主窗口对象 main.show() sys.exit(app.exec_())
加载全部内容