D
D
doktorfish00782020-10-18 19:16:54
Python
doktorfish0078, 2020-10-18 19:16:54

How to achieve visibility of objects created in Qt Designer forms when developing code in PyCharm?

I develop virtual labs in physics. I create a form in qt designer, create the necessary buttons, objects, sliders and so on in it, then I need to prescribe their actions, I do this in Python I
created a file, create a class and load my Ui form

class TestLaba(QMainWindow):
    def __init__(self):
        super(TestLaba, self).__init__()
        uic.loadUi('TEST.ui', self)

And here I want to set for example to the button some value. I am writing self. and I think, how did I call it, I climb into the form, look at the name, refer to it self.button. And I think, la, and what methods does it have in general
. I have to write QtWidjets.QButton. to see the methods by hint
Yes, I know that there is documentation and stuff, but it's so convenient when there are hints
The problem is that the python file and the class with which I am developing do not see the objects created in the form when I write the code
Of course , if I write self.button.clicked.connect(self.method_with_button_action) it will work, but I would like not to write blindfolded
I found another option, you can convert UI to Py, so I did, get a file with all variables
class Ui_Laba2(object):
    def setupUi(self, Laba2):
        Laba2.setObjectName("Laba2")
        Laba2.resize(948, 759)
        Laba2.setStyleSheet("background-color: rgb(167, 255, 207);")
        self.centralwidget = QtWidgets.QWidget(Laba2)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(50, 50, 501, 71))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.Reostat = QtWidgets.QSlider(self.verticalLayoutWidget)
        self.Reostat.setMaximum(240)
        self.Reostat.setSingleStep(1)
        self.Reostat.setSliderPosition(0)
        self.Reostat.setTracking(True)
        ........

And I imported it into my code and began to load not Ui, but Py
import labaa2

class Laba2(QMainWindow,labaa2.Ui_Laba2):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

And now if I write self. I will get hints about what objects I have.
5f8c69254597f587872077.png
And to find out what methods my slider has, it will not be necessary to write QtWidgets.QSlider. and look at the tips, but just write
5f8c6961bf23e488156886.png
And it would seem happiness, but I'm developing this application, constantly changing the form, writing test methods
And the Py file that was received when converting from Ui does not update itself, so you need to convert the file again with each change, and for this you need to open cmd write pyuic5 path.io -o path.py
Somehow not very convenient.
The article https://tproger.ru/translations/python-gui-pyqt/ says that "the file with the design will be completely overwritten every time the design is changed, we will not change it."
and it would be very nice if it did. How can this be achieved or how can work be optimized in another way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-10-19
@doktorfish0078

Get a separate field for forms in the class, it will be more convenient.

import labaa2

class Laba2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = self.setupUi(self)

Then the whole form will be in ui . The fact is that QtDesigner creates files in "its own format" (xml, if I remember correctly), try opening the ui file with notepad, you'll see. Python is not a dream or a spirit, what is there and how to interact with it, this is where uic enters the stage. It converts the ui widget tree into a valid Python class that uses the installed Python wrapper library over Qt. There are 2 approaches with uic: 1. (it makes sense to shove this into a separate class field, for example, so that there are no unnecessary intersections of names, and, in my opinion, it will turn out more accurately, you never know how to load another form into the same class. ) uic parses the ui file and creates entities. This will happen every time the program is started.
self.ui.button # например
+ Does not require intermediate links
- IDE auto-completion does not work
- Parsing and generating a python class every time you run it.
2. Convert in advance.
- Requires an intermediate action between the form and the program. (conversion to ui in py)
+ Auto-completion works
+ No time wasted on conversion when starting the program.
+ In PyCharm, you can set up hotkey conversion.
It is not necessary to inherit a class from two classes at once, in 99.9% you do not need it, but you can get strange effects for you.
Using pre-convert
Import the converted form at the very beginning, give it a meaningful name, then include
import labaa2
from labaa2.Ui_Laba2 import Ui_что_там_у_вас as MainForm_UI

class Laba2(QMainWindow,):
    def __init__(self):
        super().__init__()
        self.ui = MainFormUI()
        self.ui.setupUi(self)

now the whole form will be in ui, autocompletion works, Pycharm most likely will not see the signals, but this is normal, they will naturally work.
P.S. Pycharm and External tools
Pictures

5f8caa995f6ff805437661.png
5f8cad28bb2ce085833759.png
Для такой организации формочек
5f8cadf60e0e2969686885.png
Правой кнопкой по файлу - то, как Вы назовете external tool.
5f8caefc48637441580438.png
# импорт
import labaa2
from forms.py.temp_ui import Ui_MainWindow as MainForm


class Laba2(QMainWindow,):
    def __init__(self):
        super().__init__()
        self.ui = MainForm()
        self.ui.setupUi(self)

Добавление хоткея
5f8cafbe07d7f009801572.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question