您的位置:首页技术文章
文章详情页

python 按钮点击关闭窗口的实现

【字号: 日期:2022-08-04 10:53:51浏览:3作者:猪猪

1、简介

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 sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QApplicationclass 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_())

python 按钮点击关闭窗口的实现

可以设置 按钮位置和大小,但是速度有点慢

2)使用 python 直接写出来

速度快一些,但是还没有掌握怎么设置按钮大小和位置··

python 按钮点击关闭窗口的实现

# -*- coding: utf-8 -*- ’’’ 【简介】PyQT5中主窗口例子’’’import sysfrom PyQt5.QtWidgets import QMainWindow , QApplication, QDesktopWidget,QPushButton,QHBoxLayout,QWidgetfrom PyQt5.QtGui import QIconclass 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_())

标签: Python 编程
相关文章: