Python Forum
[PyQt] How to put class method into new module?
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
# 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_())


gui.py
# -*- 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_())


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,973 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [PyQt] I get a name Name Error: when calling a method inside a class radoo 2 2,330 Jun-11-2020, 05:02 PM
Last Post: radoo
  [Tkinter] Issue with calling a Class/Method in GUI File Fre3k 3 2,948 Mar-08-2020, 12:35 PM
Last Post: Fre3k
  [Kivy] How do I reference a method in another class? Exsul1 3 4,221 Mar-02-2020, 07:32 PM
Last Post: Exsul1
  [Tkinter] Call a class method from another (Tk GUI) class? Marbelous 3 6,140 Jan-15-2020, 06:55 PM
Last Post: Marbelous
  [Tkinter] Class with text widget method AeranicusCascadia 3 7,738 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia
  How to define a method in a class 1885 2 4,620 Oct-29-2017, 02:00 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020