Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to put class method into new module?
#1
Hello everyone, this is my first post here. Hope to get some help if possible.
I have simple script which indicates state of checkBox widget which is printing out situation of the widget. It is made with pyqt4 desighner. It's working ok, but I would like to put method "print_situation" out of the "gui.py" module into different module, for example into new module "functions_module.py". The question is how to do it, to call that function in "main.py" as "master.checkBox.clicked.connect(master.print_situation)"?

Why to do that: I am building application with quite complex GUI module and and would like to minimise number of lines in "gui.py" module.
Thanks in advance.

Vlado


main.py
[icode]# python 2.7.9

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QTimer
import gui
import sys


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)


    # Set the window from imported   GUI_test
    gui_master = gui.QtGui.QTabWidget()
    master = gui.Ui_TabWidget()
    master.setupUi(gui_master)

    # Set buttons
    master.checkBox.clicked.connect(master.print_situation)


    gui_master.show()
    sys.exit(app.exec_())[/icode]

gui.py
[icode]# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GUI_test.ui'
#
# Created: Mon Jan 16 16:47:13 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_TabWidget(object):
    def setupUi(self, TabWidget):
        TabWidget.setObjectName(_fromUtf8("TabWidget"))
        TabWidget.resize(640, 480)
        self.tab1 = QtGui.QWidget()
        self.tab1.setObjectName(_fromUtf8("tab1"))
        self.checkBox = QtGui.QCheckBox(self.tab1)
        self.checkBox.setGeometry(QtCore.QRect(280, 150, 100, 26))
        self.checkBox.setObjectName(_fromUtf8("checkBox"))
        TabWidget.addTab(self.tab1, _fromUtf8(""))
        self.tab2 = QtGui.QWidget()
        self.tab2.setObjectName(_fromUtf8("tab2"))
        TabWidget.addTab(self.tab2, _fromUtf8(""))

        self.retranslateUi(TabWidget)
        TabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(TabWidget)

    def retranslateUi(self, TabWidget):
        TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget", None))
        self.checkBox.setText(_translate("TabWidget", "CheckBox", None))
        TabWidget.setTabText(TabWidget.indexOf(self.tab1), _translate("TabWidget", "Tab 1", None))
        TabWidget.setTabText(TabWidget.indexOf(self.tab2), _translate("TabWidget", "Tab 2", None))

    def print_situation(self):
        if self.checkBox.checkState() == QtCore.Qt.Checked:
            print "Checked _1_"
        else:
            print "Unchecked _0_"
            

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    TabWidget = QtGui.QTabWidget()
    ui = Ui_TabWidget()
    ui.setupUi(TabWidget)
    TabWidget.show()
    sys.exit(app.exec_())


[/icode]
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)