geekdoc-python-zh/docs/pythonbasics/117.md

99 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Qt Designer Python
> 原文: [https://pythonbasics.org/qt-designer-python/](https://pythonbasics.org/qt-designer-python/)
Qt Designer 可以帮助您构建 GUI图形用户界面。 您可以从 Python 加载 GUI。 在本教程中,我们将逐步向您展示。
它涵盖了一个非常基本的示例,说明如何将 Qt Designer 与 PyQt 和 Python 一起使用。 有关更多详细信息,请参见下面的链接。
## Qt Designer Python
### 先决条件
要开始本教程,您需要安装以下内容:
* Python
* PyQt
* Qt Designer
您将需要 Python 3 或更高版本,因为其他版本已过时。
如果您没有 PyQt请[安装 PyQt](/install-pyqt/)。
您可以使用以下工具安装 DesignerUbuntu Linux
```py
sudo apt-get install qttools5-dev-tools
sudo apt-get install qttools5-dev
```
在其他平台上,它包含在设置中。
### 如何启动 Designer
通过在命令行中输入`designer`来启动设计器。 QT Creator 是另一个程序。
在 Ubuntu Linux 上:
```py
cd /usr/lib/x86_64-linux-gnu/qt5/bin/
./designer
```
### 基础
出现一个弹出窗口。 您可以选择要设计的东西。
![pyqt designer](img/ae8e6eb853cd61665d2bfd313a2d391e.jpg)
选择“主窗口”,然后单击“创建”。
然后,您可以调整表单的大小并拖放小部件。 设计这样的图形界面非常简单。
如果单击窗口小部件(例如按钮),则可以设置其属性,例如名称。
![qt designer](img/7b3d5c4209c210120366a607eb41d2ce.jpg)
### 将设计导出到 UI
您可以将设计导出到 UI 文件。 单击“文件 -> 另存为 -> `yourname.ui`
然后,您可以将 ui 代码转换为 python 文件。像这样:
```py
pyuic5 /home/linux/helloworld.ui -o helloworld.py
```
然后Python 文件包含 gui 定义。创建另一个加载 ui 文件的文件:
```py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
import helloworld
class ExampleApp(QtWidgets.QMainWindow, helloworld.Ui_MainWindow):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
def main():
app = QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
```
一旦运行它GUI 就会出现。
[下载示例](https://gum.co/pysqtsamples)