0

This is my main.py I have a function called loadShot that I want to call from another

class MainWindow(QMainWindow):
    # Main Window UI
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        loadUi(os.path.join(SCRIPT_DIRECTORY, 'mainwindow.ui'), self)

        self.connectInterface()

    # Connect signals
    def connectInterface(self):
        self.scene_Line.textChanged.connect(self.shotName)
        self.activeProjcet_Line.textChanged.connect(self.shotName)
        self.character_Line.textChanged.connect(self.shotName)
        self.take_Line.valueChanged.connect(self.shotName)
        self.load_Button.setShortcut(QKeySequence("Alt+B"))

    ####################################################
    #   Shot Loader Functions
    ####################################################


    def browse(self, dir):
        root = Tkinter.Tk()
        root.withdraw() #use to hide tkinter window

        tempdir = tkFileDialog.askdirectory(parent=root, initialdir=dir, title='Please select a directory')

        if tempdir.startswith('D:/Capture data/'):
            self.activeProjcet_Line.setText(tempdir)
        elif tempdir.startswith('R:/Project Files/'):
            self.uploadProjcet_Line.setText(tempdir)
            self.uploadFolder()
        else:
            pass

    def uploadFolder(self):
        project = self.activeProjcet_Line.text()
        uploadDir = self.uploadProjcet_Line.text()
        f = open('C:/_Mocap/output/folderName.txt', 'w')
        f.write(' \n' + project.replace('D:/Capture data/', '') + '\n' + uploadDir.replace('R:/Project Files/', ''))
        f.close()

    def loadShot(self):
        shot = self.shotName_Line.text()
        f = open('C:/_Mocap/output/ShotLoader.txt', 'w')
        f.write('\n' + '[name]\n' + '\n' + 'take Name=' + shot)
        f.close()
        self.uploadFolder()
        if self.incrementTake.isChecked():
            self.takeIncrement()
        else:
            pass

This is my other python file that is a key listener and I want to loadShot function. The problem is I keep loading the MainWindow as an instance. Which I cannot do. I need to just be able to call the function in my MainWindow class without loading another instance.

def handle_Ctrl_L ():
    m = MainWindow()
    m.loadShot()
    hk = HotKeys()
    w = WindowMgr()
    pid = w.GetProcessID('Blade')
    w.focusWindow(pid)
    time.sleep(.2)
    hk.F8()
101
  • 8,514
  • 6
  • 43
  • 69
  • It's not 100% clear what you're asking, but it sounds like you want to call a bound method (a method that has `self` as first parameter, in your case `loadShot`) without instantiating your class. You can't do that. You could make that method a class method but then you'd lose the reference to instance variables like `self.shotName_Line.text()` etc. Can you expand on what you're actually trying to achieve a little? – 101 Apr 28 '15 at 23:41
  • Thanks for replying. I have my main.py which has my pyside gui MainWindow. I have a few methods that interact with the gui. loadShot() gets info from a line edits and writes it to a file. In my other.py file. I have a keyboard listener which has the method handle_CTRL_L () I want to be able to call the loadShot() method. This can't be an instance because it relaunches my gui. Killing all info and writes nothing to the file. I was hoping to be able to use a custom signal under the handle_ctrl_L() method that emits when the method is called. Thanks. – Peter Collazo Apr 29 '15 at 13:39

2 Answers2

0

In one project i had to enable one module to do a callback to my Mainwindow module. The Controller of the Mainwindow view starts a new subprocess and retrieves the stdout out as well as the callback as soon as the program is terminated. I managed this in the following way: (Maybe it helps with your problem, which i not totally understand)

MainWindow Module:

def run_program():
    # consoleprocess is the second module that 
    # has to be able to do a callback. 
    # A new Instance of the ProcessRunner class is created.

    self.progrunner = consoleprocess.ConsoleProcessRunner()
    self.progrunner.cout.connect(self.cb_update_prog_output)
    self.progrunner.quit.connect(self.cb_prog_terminated)

@QtCore.Slot(int)
@QtCore.Slot(str)
def cb_update_tv(self, data):
    # your code goes here
    pass

Second Module (consoleprocess):

# The Class has to inherit QtCore.Object

class ConsoleProcessRunner(QtCore.QObject):
    # The Signals that allow the callback are declared

    cout = QtCore.Signal(str)
    quit = QtCore.Signal(int)

    # Constructor
    def __init__(self, parent = None):
        super(ConsoleProcessRunner, self).__init__(parent)


    def your_function_here():
        # now you can use our previously defined signals to do the callback
        # your code goes here
        self.cout.emit(stdoutdata)
        self.quit.emit(ret) 
Ben
  • 783
  • 7
  • 13
0

If your application only needs one instance of MainWindow, then you could achieve what you want by making it a singleton class:

class MainWindow(QMainWindow):
    _instance = None
    _initialized = False

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(MainWindow, cls).__new__(cls)
        return cls._instance

    def __init__(self, parent=None):
        if not self._initialized:
            super(MainWindow, self).__init__(parent)
            loadUi(os.path.join(SCRIPT_DIRECTORY, 'mainwindow.ui'), self)
            self.connectInterface()     
            self._initialized = True        

And now whenever MainWindow() is called, it will always return the same instance.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • PS: Why the heck are you using a `Tkinter` file-dialog? What's wrong with [`QFileDialog`](http://doc.qt.io/qt-4.8/qfiledialog.html)? – ekhumoro Apr 30 '15 at 18:52
  • Yea I know it was old code from a test. I have switched it out for the QFILEDIALOG. Thanks – Peter Collazo May 05 '15 at 00:44