0

I want to call method (close_ok()) in (main.py ) from method :close_call() in (module_b.py), but the Code does not work successfully when the function is called. can anyone help me with this problem? Here's the code for that :

@@@main.py

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
from GUI import Ui_MainWindow
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        from module_c import class_c
        global b 
        b=class_c()
        QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )
    def close_ok(self):
        ##But it can not be done successfully.
        self.close()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    global myapp
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

@@@ module_b.py

class class_c (object):
     def __init__(self, parent=None):
       self.parent=parent

 ### I want call method (close_ok()) in (menu class) from here    
     def close_call (self):
          from main import MainWindow
          t=MainWindow()
          t.close_ok()

@@@GUIpy

# -*- coding: utf-8 -*-


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_MainWindow(object):
    def setupUi(self, MainWindow):
        super(Ui_MainWindow, self).__init__()
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340, 110, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "PushButton", None))
Ahmad Ali
  • 57
  • 1
  • 8
  • "Not work" how exactly? Do you get an error? Does it run, but do nothing? If you put a `print` in `close_ok` does it print to the console? – Tim S. Dec 26 '15 at 14:37
  • when i put print in (close_ok), The Code does work successfully. but it does not work in GUI. – Ahmad Ali Dec 26 '15 at 14:52

1 Answers1

0

you use diffrent instances of the class MainWindow() so it wont close the original window, change it like this:

class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        from module_c import class_c
        global b 
        b=class_c(self) #NOTICE THE CHANGE HERE
        QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )

    def close_ok(self):
        self.close()

and change module b to do:

class class_c (object):
 def __init__(self, parent=None):
   self.parent=parent

 def close_call (self):
      from main import MainWindow
      self.parent.close_ok()# NOTICE THIS CHANGE, parent is now the original MainWindow

this way you pass your original MainWindow instancce to class_c and use it, instead of creating a new instance

DorElias
  • 2,243
  • 15
  • 18
  • Traceback (most recent call last): File "D:\python-project\Q3\module_c.py", line 8, in close_call self.parent.close_ok() AttributeError: 'NoneType' object has no attribute 'close_ok' – Ahmad Ali Dec 26 '15 at 14:47
  • this you change this as well: `b=class_c(self)` (so parent wont be None)? – DorElias Dec 26 '15 at 14:48
  • DorElias >>> Can you help me here ? http://stackoverflow.com/questions/34482374/attributeerror-class-c-object-has-no-attribute-lineedit?noredirect=1#comment56706675_34482374 – Ahmad Ali Dec 27 '15 at 16:52