設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 335|回復: 0
打印 上一主題 下一主題

PyQt5 Tutorial / Creating applications with Qt Designer

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2023-3-28 21:23:26 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

The first step in building an application is to add some widgets to your window. In our first applications we learnt that to set the central widget for a QMainWindow we need to use .setCentralWidget(). We also saw that to add multiple widgets with a layout, we need an intermediary QWidget to apply the layout to, rather than adding the layout to the window directly.
Qt Creator takes care of this for you automatically, although it's not particularly obvious about it.
To add multiple widgets to the main window with a layout, first drag your widgets onto the QMainWindow. Here we're dragging 3 labels. It doesn't matter where you drop them.
Main window with 1 labels and 1 button added.
We've created 2 widgets by dragging them onto the window, made them children of that window. We can now apply a layout.
Find the QMainWindow in the right hand panel (it should be right at the top). Underneath you see centralwidget representing the window's central widget. The icon for the central widget show the current layout applied. Initially it has a red circle-cross through it, showing that there is no layout active.
Right click on the QMainWindow object, and find 'Layout' in the resulting dropdown.
Right click on the main window, and choose layout.
Next you'll see a list of layouts which you can apply to the window. Select Lay Out Horizontally and the layout will be applied to the widget.
Select layout to apply to the main window.
The selected layout is applied to the the centralwidget of the QMainWindow and the widgets are added the layout, being laid out depending on the selected layout. Note that in Qt Creator you can actually drag and re-order the widgets within the layout, or select a different layout, as you like. This makes it especially nice to prototyping and trying out things.
Vertical layout applied to widgets on the main window.
Using your generated .ui file
We've created a very simple UI. The next step is to get this into Python and use it to construct a working application.
First save your .ui file — by default it will save at the location you chosen while creating it, although you can choose another location if you like.
The .ui file is in XML format. To use our UI from Python we have two alternative methods available —
  • load into into a class using the .loadUI() method
  • convert it to Python using the pyuic5 tool.
These two approaches are covered below. Personally I prefer to convert the UI to a Python file to keep things similar from a programming & packaging point of view.
Loading the .ui file directly
To load .ui files we can use the uic module included with PyQt5, specifically the uic.loadUI()method. This takes the filename of a UI file and loads it creating a fully-functional PyQt5 object.
[color=rgba(255, 255, 255, 0.3)][size=0.7]PYTHONimport sysfrom PyQt5 import QtWidgets, uicapp = QtWidgets.QApplication(sys.argv)window = uic.loadUi("mainwindow.ui")window.show()app.exec()
A (very) simple UI designed in Qt Creator
As the uid.loadUI() method turns an instance object you cannot attach custom __init__() code. You can however handle this through a custom setup function
To load a UI from the __init__ block of an existing widget (e.g. a QMainWindow) you can use uic.loadUI(filename, self) for PyQt5.
  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. from PyQt5 import uic


  4. class MainWindow(QtWidgets.QMainWindow):

  5.     def __init__(self, *args, **kwargs):
  6.         super().__init__(*args, **kwargs)
  7.         uic.loadUi("mainwindow.ui", self)


  8. app = QtWidgets.QApplication(sys.argv)
  9. window = MainWindow()
  10. window.show()
  11. app.exec_()
複製代碼

Converting your .ui file to Python
To generate a Python output file run pyuic5 from the command line, passing the .ui file and the target file for output, with a -o parameter. The following will generate a Python file named MainWindow.py which contains our created UI.
  1. pyuic5 mainwindow.ui -o MainWindow.py
複製代碼

If you're using PyQt4 the tool is named `pyuic4`, but is otherwise completely identical.
You can open the resulting MainWindow.py file in an editor to take a look, although you should not edit this file. The power of using Qt Creator is being able to edit, tweak and update your application while you develop. Any changes made to this file will be lost when you update it. However, you can override and tweak anything you like when you import and use the file in your applications.
Importing the resulting Python file works as for any other. You can import your class as follows. The pyuic5 tool appends Ui_ to the name of the object defined in Qt Creator, and it is this object you want to import.
  1. from MainWindow import Ui_MainWindow
複製代碼

To create the main window in your application, create a class as normal but subclassing from both QMainWindow and your imported Ui_MainWindow class. Finally, call self.setupUi(self) from within the __init__ to trigger the setup of the interface.
  1. import sys
  2. from PyQt5 import QtWidgets, uic

  3. from MainWindow import Ui_MainWindow


  4. class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  5.     def __init__(self, *args, obj=None, **kwargs):
  6.         super(MainWindow, self).__init__(*args, **kwargs)
  7.         self.setupUi(self)


  8. app = QtWidgets.QApplication(sys.argv)

  9. window = MainWindow()
  10. window.show()
  11. app.exec()
複製代碼

This produces exactly the same result as before.
A (very) simple UI designed in Qt Creator
That's it. Your window is now fully set up. Since the use of a .ui file abstracts out the UI-specific code, you can use this same pattern to load any interface you design.

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-16 22:19 , Processed in 0.213273 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表