UI design - design shortcut template for pycharm

When using pycharm combined with qt designer to design gui, some codes are common, and you can set this part of the code as a shortcut template.

  1. Click File-Setting in turn in the software, as shown in the figure
    insert image description here

  2. Find the Live Templates as shown in the figure below, and expand the Python item in it
    insert image description here

  3. Click the plus sign on the right and select Live Templates
    insert image description here

  4. The red box 1 in the figure below is the shortcut abbreviation of the shortcut template, for example, you can write qto. 2 is the description of the shortcut template. 3 places are the content of the shortcut template.
    insert image description here

  5. Fill in the following code into the red box 3 in the above picture. title title in the codet i tl e is actually the file name of the py file after the ui file is converted.

# 日期:  2023/4/17 14:45
from PyQt5.Qt import *
from $title$ import Ui_Form


class Window(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setup_ui()     # 渲染画布

    def setup_ui(self):
        self.setupUi(self)  # 调用Ui_Form的setupUi渲染界面


if __name__ == "__main__":
    import sys
    from PyQt5 import QtCore
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)     # 设置支持高分辨率屏幕自适应,防止界面乱板,一般在程序入口添加
    app = QApplication(sys.argv)
    mywindow = Window()
    mywindow.show()
    sys.exit(app.exec_())

Explanation of the code above:

  • app = QApplication(sys.argv): Create a Qt application object, QApplication, and pass the command-line argument sys.argv to it. QApplication is a core class in the PyQt library, which is used to manage the event loop and GUI thread of Qt applications.

  • mywindow = Window(): Create an instance object mywindow of a custom window class Window, which is used to display the main window of the application.

  • mywindow.show(): Display the custom window object mywindow on the screen. show() is a method of the QWidget class, used to display the window object.

  • sys.exit(app.exec_()): Enter the main event loop of the Qt application, waiting for the occurrence of user interaction events. When the user closes the main window or calls the QApplication.quit() method, the event loop ends, the app.exec_() method returns an exit status code, and then passes the status code to the operating system through the sys.exit() method to end the application program running.

  1. Apply the above template to python code writing, set as shown in the figure below, and check python in the red box 2.
    insert image description here

  2. After the above settings, when writing the code, you can directly input qto, and the pycharm software will prompt the shortcut template. At this time, just click Enter to enter the template.
    insert image description here

Note : In the above code, the created class uses multiple inheritance, but it must be noted that the window type created by using qt designer must be the same as another inherited class. For example, the window created by qt designer above is of Qwidget type, so another inheritance type of multiple inheritance in the above code is also Qwidget. If the window type created by qt designer is QMainWindow, the code should be class Window(QMainWindow, Ui_MainWindow):, that is, another class inherited should also be QMainWindow.

Reference:
https://blog.csdn.net/g11023225/article/details/119274622?spm=1001.2014.3001.5506
https://zhuanlan.zhihu.com/p/401503085

Guess you like

Origin blog.csdn.net/ThreeS_tones/article/details/130200293