python按钮点击关闭窗口的实现-创新互联

1、简介

创新互联-专业网站定制、快速模板网站建设、高性价比乌恰网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式乌恰网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖乌恰地区。费用合理售后完善,10年实体公司更值得信赖。

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_())

分享名称:python按钮点击关闭窗口的实现-创新互联
当前地址:http://scyanting.com/article/cocspg.html