PyQt5 – QApplication

PyQt5 – QApplication

QApplication类管理GUI应用程序的控制流和主要设置。它专门为QGuiApplication提供了基于QWidget的应用程序所需的一些功能。它处理小部件的具体初始化、最终化。对于任何使用Qt的GUI应用程序,无论该应用程序在任何时候有0、1、2或更多的窗口,都有精确的QApplication对象。对于非基于Qwidget的Qt应用程序,使用QGuiApplication代替,因为它不依赖于QtWidgets库。

然后我们创建一个窗口实例,并在事件循环中使用sys.exit(App.exec())命令执行QApplication对象,下面是一些有用的和经常使用的QApplication对象的方法和属性。

语法: App = QApplication(sys.argv)

参数

  • beep: 发出铃声,使用默认的音量和声音。这个函数在Qt for Embedded Linux中不可用。
  • setFont: 它设置了PyQt5应用程序的默认字体。
  • aboutQt: 显示一个关于Qt的简单消息框。该消息包括应用程序正在使用的Qt的版本号。
  • closeAllWindows: 关闭所有顶层窗口。这个函数对有许多顶层窗口的应用程序特别有用。
  • setAutoSipEnabled: 当进入接受键盘输入的部件时,它自动显示SIP。
  • setCursorFlashTime: 这个方法设置文本光标的闪动(闪烁)时间,单位是毫秒。
  • setDoubleClickInterval: 该方法设置区分双击和连续两次鼠标点击的时间限制,单位是毫秒。

例子

我们将创建一个简单的PyQt5应用程序,当它被执行时产生嘟嘟声,许多属性被设置到QApplication对象上,下面是实现方法

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 500, 400)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating a push button
        push = QPushButton("Press", self)
 
        # setting geometry to the push button
        push.setGeometry(100, 100, 120, 40)
 
        # creating a label
        label = QLabel("GeeksforGeeks", self)
 
        # setting geometry to the label
        label.setGeometry(100, 160, 200, 50)
 
        # setting alignment to the label
        label.setAlignment(Qt.AlignCenter)
 
        # font
        font = QFont("Arial", 12)
 
        # setting font to the label
        label.setFont(font)
 
        # setting style sheet to the label
        label.setStyleSheet("QLabel"
                            "{"
                            "border : 2px solid green;"
                            "background : lightgreen;"
                            "}")
 
        # hiding the label
        label.hide()
 
        # adding action method to the push button
        push.clicked.connect(lambda: do_something())
 
        # method called by the push button when pressed
        def do_something():
 
            # unhide the label
            label.show()
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# setting cursor flashtime
App.setCursorFlashTime(100)
 
# setting application object name
App.setObjectName("GfG")
 
# setting application display name
App.setApplicationDisplayName("GfG PyQt5")
 
# beep sound will occur when application
# is opened
App.beep()
 
# message displayed about the Qt
App.aboutQt()
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())

输出:

当我们执行 代码时 ,首先会显示关于Qt的页面。

PyQt5 - QApplication

然后我们的应用程序就会开始了

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程