PyQt5的基础知识

107 阅读7分钟

PyQt5是一个用于GUI桌面应用开发的python模块。它可用于多种平台,如Windows、Mac、Linux、iOS和Android。事实上,Python提供了几个能够进行GUI开发的模块,如Tkinter、wxPython、PySide2等等。然而,PyQt5利用了1000多个类;事实上,PyQt5是一个巨大的模块此外,PyQt5包括一个Qt Designer,一个图形用户界面设计器,它进一步促进了GUI的创建。它可以用来创建从媒体播放器到网络浏览器的任何东西。在本教程中,我们将学习PyQt5模块的基础知识。

首先,我们来安装PyQt5。

pip install pyqt5

pip install pyqt5-tools

第1步:创建空白窗口

创建任何东西的第一步是设置空白窗口。空白窗口本身需要几行代码,所以我们来看看。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow

import sys

fromPyQt5importQtGui

classwindow(QtWidgets.QWidget):

def __init__(self):

super().启用

使用setGeometry()方法设置窗口的几何形状,该方法需要四个参数--初始X位置、初始Y位置(换句话说,屏幕上左上角的位置)、窗口的宽度和高度。

self.setGeometry(350, 100, 800, 600)

用setWindowTitle()方法设置窗口的标题。

self.setWindowTitle( "PyQt5" )

你可以用setWindowIcon()来设置图标。注意,图标必须是64像素×64像素。

self.setWindowIcon(QtGui.QIcon("rattle.png"))

每个PyQt5文件都需要下面这一行,它需要sys.argv作为参数。

application=QApplication(sys.argv)

接下来,创建一个我们上面创建的类的实例。

win= window()

win.show()

为了通过按X按钮退出窗口,我们需要sys.exit(application.exec())。

sys.exit(application.exec())

这段代码将创建一个空白窗口。整个代码看起来是这样的。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow

import sys

fromPyQt5importQtGui

classwindow(QtWidgets.QWidget):

def __init__(self):

super().__init__()

self.setGeometry(350, 100, 800, 600)

self.setWindowTitle("PyQt5")

self.setWindowIcon(QtGui.QIcon("rattle.png")

application=QApplication(sys.argv)

win= window()

win. show()

sys.exit(application.exec() )

第二步:输入栏

接下来,让我们创建一个输入栏。输入栏是一个用户可以添加文本的地方,我们可以检索到这些文本。输入栏是用QWidgets.QLineEdit()创建的。显然,我们使用setGeometry()方法来设置它的几何形状。

definitUI(self):

self.input_bar =QtWidgets.QLineEdit(self)

self.input_bar.setGeometry(150, 250, 500, 40)

请注意;你仍然必须在__init__方法中激活这个函数,如下所示。

self.initUI()

这时的完整代码看起来是这样的。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow

import sys

fromPyQt5importQtGui

classwindow(QtWidgets.QWidget):

def __init__(self):

super().__init__()

self.setGeometry(350, 100, 800, 600)

self.setWindowTitle("PyQt5")

self.setWindowIcon(QtGui.QIcon("rattle.png")

self.initUI()

definitUI(self):

self.input_bar =QtWidgets.QLineEdit(self)

self.input_bar.setGeometry(150, 250, 500, 40)

application=QApplication(sys.argv)

win= window()

win. show()

sys.exit(application.exec() )

第3步:创建按钮

现在,让我们在空白画布上添加一些按钮。所以,让我们为按钮编写代码。对于这个按钮,我们使用QtWidgets.QPushButton()。像往常一样,我们可以使用setGeometry()方法来设置它的几何形状。

self.button1 =QtWidgets.QPushButton("Show", self)

self.button1.setGeometry(275, 350, 200, 50)

使用setIcon()方法设置图标。

self.button1.setIcon(QtGui.QIcon("rattle.png"))

使用setStyleSheet()方法设置文本的样式。你可以改变颜色、字体重量和字体大小,以及其他。

self.button1.setStyleSheet("color:black")

self.button1.setStyleSheet("font-weight: bold")

self.button1.setStyleSheet("font-size: 18pt")

为了让按钮在被点击时做一些事情,你需要告诉按钮,它需要在被点击时激活一个函数。这可以用clicked.connect()来完成,其中被激活的函数被作为参数传递。在我的例子中,它是

self.button1.clicked.connect(self.button_clicked)

接下来,我们定义当按钮被按下时要调用或激活的函数。现在,我们只是在控制台中打印出来。

defbutton_clicked(self):
url_value= self.input_bar. text()

print(url_value)

现在的代码整体看起来是这样的。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow

import sys

fromPyQt5importQtGui

classwindow(QtWidgets.QWidget):

def __init__(self):

super().__init__()

self.setGeometry(350, 100, 800, 600)

self.setWindowTitle("PyQt5")

self.setWindowIcon(QtGui.QIcon("rattle.png")

self.initUI()

definitUI(self):

self.input_bar =QtWidgets.QLineEdit(self)

self.input_bar.setGeometry(150, 250, 500, 40)

self.button1 =QtWidgets.QPushButton("Show", self)

self.button1.setGeometry(275, 350, 200, 50)

self.button1.setIcon(QtGui.QIcon("rattle.png")

self.button1.setStyleSheet("color:black")

self.button1.setStyleSheet("font-weight: bold")

self.button1.setStyleSheet("font-size: 18pt")

self.button1.clicked。button1.clicked.connect(self.button_clicked)

defbutton_clicked(self):

url_value= self.input_bar.text()

print(url_value)

application=QApplication(sys.argv)

win= window()

win. show()

sys.exit(application.exec() )

第四步:创建标签

现在让我们使用QLabels来修改按下按钮的命令。QLabels是用来添加文本的。 我们把这个添加到def initUI(self)。

self.label =QtWidgets.QLabel(self)

我们使用setText()方法设置标签上的文本。

self.label.setText("Change This Title by Clicking the Button")

self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

我们使用setStyleSheet()来设置字体、大小和权重。

self.label.setStyleSheet("font-weight:bold")

self.label.setStyleSheet("font-size: 18pt" )

最后,我们使用update()方法更新所有内容。

self.label.update()

这将创建以下内容。

现在,我们可以在button_clicked()函数中改变内容。

defbutton_clicked(self):

我们可以使用text()方法检索用户在文本栏中写的内容。

url_value= self.input_bar.text()

然后我们可以使用setText()方法在点击按钮时改变标签,并使用setGeometry()方法将它们放置在正确的位置。

self.label.setText(url_value)

self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

现在的代码整体看起来是这样的。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow

import sys

fromPyQt5importQtGui,QtCore

classwindow(QtWidgets.QWidget):

def __init__(self):

super().__init__()

self.setGeometry(350, 100, 800, 600)

self.setWindowTitle("PyQt5")

self.setWindowIcon(QtGui.QIcon("rattle.png")

self.initUI()

definitUI(self):

self.input_bar =QtWidgets.QLineEdit(self)

self.input_bar.setGeometry(150, 250, 500, 40)

self.button1 =QtWidgets.QPushButton("Show", self)

self.button1.setGeometry(275, 350, 200, 50)

self.button1.setIcon(QtGui.QIcon("rattle.png")

self.button1.setStyleSheet("color:black")

self.button1.setStyleSheet("font-weight: bold")

self.button1.setStyleSheet( "font-size: 18pt")

self.button1.clicked.connect(self.button_clicked)

self.label =QtWidgets.QLabel(self)

self.label.setText("Change This Title by Clicking the Button")

self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

self.label.setStyleSheet("font-weight:bold")

self.label.setStyleSheet("font-size: 18pt")

self.label.update()

defbutton_clicked(self):

url_value= self.input_bar.text()

self.label.setText(url_value)

self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

application=QApplication(sys.argv)

win= window()

win. show()

sys.exit(application.exec() )

第5步。qvboxlayout和qhboxlayout

不会在这里添加QVBoxlayout或QHBoxlayout,但如果你想的话可以。QHBoxLayout将以水平模式排列所有东西,而QVBoxLayout将垂直排列。如果你使用QHBoxLayout或QVBoxLayout,你必须省略setGeometry()方法。

如果你想添加它,你可以在def initUI(self)中写如下。你首先使用QVBoxLayout()初始化布局。

self.design =QVBoxLayout()

然后你使用addWidget()方法在其中添加你想要的部件。

self.design.addWidget(self. label)

self.design.addWidget(self.input_bar)

self.design.addWidget(self.button1)

你可以使用setLayout()来设置布局,它以初始化的变量为参数。

self.setLayout(self.design)

我在这里不需要它,因为我用setGeometry()设置了所有的东西,所以我将在我的代码中省略这个。然而,如果你想在你的代码中使用它,整个代码将看起来像这样。

fromPyQt5importQtWidgets

fromPyQt5.QtWidgets importQApplication,QMainWindow,QHBoxLayout,QVBoxLayout

import sys

fromPyQt5importQtGui,QtCore

classwindow(QtWidgets.QWidget):

def __init__(self):

super().__init__()

#self.setGeometry(350, 100, 800, 600)

self.setWindowTitle("PyQt5")

self.setWindowIcon(QtGui.QIcon("rattle.png")

self.initUI()

definitUI(self):

self.input_bar =QtWidgets.QLineEdit(self)

#self.input_bar.setGeometry(150, 250, 500, 40)

self.button1 =QtWidgets.QPushButton("Show", self)

#self.button1.setGeometry(275, 350, 200, 50)

self.button1.setIcon(QtGui.QIcon("rattle.png")

self.button1.setStyleSheet("color:black")

self.button1.setStyleSheet( "color:black")button1.setStyleSheet("font-weight: bold")

self.button1.setStyleSheet( "font-size: 18pt")

self.button1.clicked.connect(self.button_clicked)

self.label =QtWidgets.QLabel(self)

self.label.setText("Change This Title by Clicking the Button")

#self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

self.label.setStyleSheet("font-weight:bold")

self.label.setStyleSheet("font-size: 18pt")

self.label.update()label.update()

self.design =QVBoxLayout()

self.design。design.addWidget(self.label)

self.design.addWidget(self.input_bar) self.input_bar)

self.design.addWidget(self.button1)

self.setLayout(self. design)design)

defbutton_clicked(self):

url_value= self.input_bar.text()

self.label.setText(url_value)

self.label.setGeometry(QtCore.QRect(200, 80, 500, 100))

application=QApplication(sys.argv)

win= window()

win. show()

sys.exit(application.exec() )

第6步。QT设计器

PyQt5更棒的地方在于它自带了设计器。设计器是一个控制台,你可以在那里设计你想要的GUI,程序将为它提供python代码。Qt Designer包含在pyqt5-tools软件包中,因此必须安装它才能工作。在Qt设计器上,你可以放置按钮、滑块等....,一旦你放置了它们,你可以将文件保存为.ui文件。

一旦文件被保存为.ui文件,你仍然需要将其转换为.py文件,以便PyCharm能够显示它。要做到这一点,打开终端或cmd,然后输入

pyuic5 -x{saved_file_name.ui}-o{python_file.py}。

我把我的文件保存为 saved_file_name.ui。终端会夹出一个Python文件,并按照你的要求调用。然后你可以在PyCharm中打开这个.py文件,并在其中添加逻辑。

请记住,尽管我们可以使用设计器来设计GUI的布局,但我们仍然需要在代码中添加逻辑,这完全是通过python代码而不是设计器来完成的。不幸的是,Qt设计器并没有向代码中添加逻辑!

在本教程中,我们了解了PyQt5的基础知识以及如何使用Qt设计器。我们了解到,我们可以创建空白屏幕,使用QPushButton添加按钮,使用QLineEdit添加输入条,使用QLabels添加文本,并使用QVBoxLayout/QHBoxLayout安排一切。事实上,PyQt5是一个非常大的模块,用于创建各种GUI桌面应用程序。尽管Python中有许多GUI应用程序的模块,但大多数人选择PyQt5是因为它提供了大量的设计选择和一个设计器来促进任务。的确,PyQt5是一个值得学习的模块!

编码愉快!