Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to move class method out of gui modul



  • Picture of GUI is missing ...

    alt text



  • Ok, this time succesed.
    alt text
    alt text
    alt text


  • Lifetime Qt Champion

    Just a quick note, you do realise that you are modifying a file which clearly states in its header that it can be regenerated and all your modification lost ?



  • Back with 3 modules. Take a look please

    main_1

    # python 2.7.9
    
    from PyQt4 import QtCore, QtGui
    from gui_1 import Ui_Form
    from apps_1 import my_app
    import sys
    
    
    if __name__ == "__main__":
    
        app = QtGui.QApplication(sys.argv)
    
        #a = my_app.print_situation(Ui_Form)
    
        gui_master = QtGui.QWidget()
        master = Ui_Form()
        master.setupUi(gui_master)
    
        master.radioButton.clicked.connect(my_app.print_situation)
    
        gui_master.show()
        sys.exit(app.exec_())
    
    

    gui_1

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'gui_2.ui'
    #
    # Created: Tue Jan 31 18:01:54 2017
    #      by: PyQt4 UI code generator 4.11.2
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt4 import QtCore, QtGui
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName(_fromUtf8("Form"))
            Form.resize(300, 150)
            self.radioButton = QtGui.QRadioButton(Form)
            self.radioButton.setGeometry(QtCore.QRect(100, 75, 118, 26))
            self.radioButton.setObjectName(_fromUtf8("radioButton"))
    
            self.retranslateUi(Form)
            QtCore.QMetaObject.connectSlotsByName(Form)
    
        def retranslateUi(self, Form):
            Form.setWindowTitle(_translate("Form", "Form", None))
            self.radioButton.setText(_translate("Form", "RadioButton", None))
    
    #    def print_situation(self):
    #        if self.radioButton.isChecked():
    #            print "Checked"
    #        else:
    #            print "Unchecked"
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        Form = QtGui.QWidget()
        ui = Ui_Form()
        ui.setupUi(Form)
        Form.show()
        sys.exit(app.exec_())
    

    apps_1

    # python 2.7.9
    from PyQt4 import QtCore, QtGui
    from gui_1 import Ui_Form
    
    
    class my_app(QtGui.QRadioButton, Ui_Form):
        def __init__(self, parent=None):
            super(my_app, self).__init__(parent)
            self.setupUi(self)
            self.radioButton = QtGui.QRadioButton()
    
        def print_situation(self):
            if self.radioButton.isChecked():
                print "Checked"
            else:
                print "Unchecked"
    

    error message

    Traceback (most recent call last):
      
    File "/home/pi/Desktop/Qt4/Project/01/Test_Class_2/main_1.py", line 19, in <module>
        
    master.radioButton.clicked.connect(my_app.print_situation)
    
    AttributeError: 
    'function' object has no attribute '__pyqtSignature__'
    

  • Lifetime Qt Champion

    @California You should not move anything out of gui.py as it is auto-generated (as @SGaist already said). You should never touch auto-generated stuff.



  • @jsulm copy that. What is max size of the module to work properly?


  • Lifetime Qt Champion

    @California There is no such max size. But for your own modules you should try to keep them small (not more than some hundreds of lines if possible), else it will be hard to understand them or change something. How big auto-genereated modules are doesn't matter as you should not change anything there and usually you do not want to read this code.



  • @jsulm In my case gui.py is quite big because all methods related to GUI must be inside GUI class. The problem is, project is not finished yet, so this module will be even bigger after some time.
    About diagrams, what tool or library should be used in my project?


  • Lifetime Qt Champion

    @California Is it really that important how big this auto-generated gui.py is?
    Diagrams? See QtCharts.



  • @jsulm When QTCreator is used, your code is big as my very fast. Tab controll with few pages produce plenty of lines of code ...
    Ok, few words about my project.
    My project involves RaspberryPi as a Master in RS485 network. This application is like a sort of SCADA for home. On the other side of the network there are slaves responsible for diferent things like temperatures, humidity, consumption of watter, electricity, ...
    So Master collets those data, do some calcullations, diagrams, storing data, ... , and most important do some graphic visualisation of everything in network.
    At the momment my code covers three slaves, but there are three slaves more to include into aplication. Each slave contains Microchip microcontroller PIC18F458 ...
    Slaves are already in function in real situation.


  • Lifetime Qt Champion

    @California To keep your gui.py smaller you can create one widget for each tab in its own file (using designer, widget_1.py, widget_2.py, ...). Then add those widgets to the tabs.



  • @jsulm that would be very usefull, do you have any example of how to do it or where it could be find to look?


  • Lifetime Qt Champion

    What are your skills level with python ?



  • @SGaist I started to learn python six months ago with my project described above


  • Lifetime Qt Champion

    So you know how to split your code in several logical modules/files and import them to use them ?



  • @SGaist No probles with importing functions, more troubles with classes, specially with everything related to gui.
    My project is unique and it's not comercial.



  • Smal change in function makes script running without an error but only "Unchecked" is printed out when radiobutton is checked or it is unchecked. How to fix this to have correct respond?

    # python 2.7.9
    from PyQt4 import QtCore, QtGui
    #from gui_1 import Ui_Form
    
    
    def print_situation(*args):
        radioButton = QtGui.QRadioButton()
        if radioButton.isChecked():
            print "Checked"
        else:
            print "Unchecked"
    

  • Lifetime Qt Champion

    Importing functions or classes is exactly the same process.

    The thing that you seem to want to do is take apart class functions

    Take a look at the 3rd example from the PyQt 5 designer introduction documentation.

    Basically, you have your generated file from pyuic5, then you create your own class that will use the generated code. Finally a main.py where you will instantiate your custom widget(s) and start the application.


  • Lifetime Qt Champion

    Hi
    Wont

    def print_situation(*args):
        radioButton = QtGui.QRadioButton()
        if radioButton.isChecked():
            print "Checked"
        else:
            print "Unchecked"
    

    radioButton = QtGui.QRadioButton()
    create a new QRadioButton

    Each time and its unchecked by default.

    Hence it always says "Unchecked" ?



  • @mrjj you are right I suppose

    @SGaist I tried to follow your point but ...
    code:

    # python 2.7.9
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QCheckBox
    #from gui_5 import Ui_Form
    
    
    class My_app(QCheckBox):
        def __init__(self):
            super(My_app, self).__init__()
            # Set up the user interface from Designer.
            self.setupUi(self)
            self.checkBox = QtGui.QCheckBox(self)
            # Connect up the buttons.
            self.checkBox.clicked.connect(self.accept)
    
        def print_situation(self, *args):
            if self.checkBox.checkState() == QtCore.Qt.Checked:
                print "Checked"
            else:
                print "Unchecked"
    

    error:

    Traceback (most recent call last):
      File "/home/pi/Desktop/Qt4/Project/01/Test_Class/main_5.py", line 17, in <module>
        master.checkBox.clicked.connect(My_app.print_situation)
    AttributeError: 'function' object has no attribute '__pyqtSignature__'
    

  • Lifetime Qt Champion

    Why are you deriving from a QCheckBox and then create a new checkbox in it ?



  • Would you be so kind to rewritte this code to loose error, because I am lost at the momment.
    A lot of time was spent for this issue in last month and I didn't make any progress at all.



  • I will take no respond as this issue is mission impossible.
    Thanks for your help.

    Vlado


  • Lifetime Qt Champion

    @California said in How to move class method out of gui modul:

    mission impossible.

    Well you seem to lack (some ?) understanding of classes in python.
    https://www.tutorialspoint.com/python/python_classes_objects.htm

    I think all the issue comes from that. Using Qt does not change anything.

    So maybe stop with Qt for a day. Try to use own objects like you want to with Qt.

    If you do not know how to make instance of a class or how to use them as parameters etc then
    its hard to split to extra files.

    Just saying. Im even bigger Beginner with python. :)



  • Thanks for this useffull link.


Log in to reply