如何从另一个 python 脚本调用 pyside MainWindow 中的函数

How to call a function in pyside MainWindow from another python script

这是我的 main.py 我有一个名为 loadShot 的函数,我想从另一个

调用它
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

这是我的另一个 python 文件,它是一个关键侦听器,我想加载 loadShot 函数。问题是我一直将 MainWindow 作为实例加载。我做不到。我只需要能够在我的 MainWindow class 中调用函数而不加载另一个实例。

def handle_Ctrl_L ():
    m = MainWindow()
    m.loadShot()
    hk = HotKeys()
    w = WindowMgr()
    pid = w.GetProcessID('Blade')
    w.focusWindow(pid)
    time.sleep(.2)
    hk.F8()

在一个项目中,我不得不启用一个模块来回调我的 Mainwindow 模块。主窗口视图的控制器启动一个新的子进程,并在程序终止后立即检索标准输出和回调。我通过以下方式管理它:(也许它有助于解决您的问题,我不完全理解)

主窗口模块:

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

第二个模块(控制台进程):

# 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) 

如果您的应用程序只需要 MainWindow 的一个实例,那么您可以通过将其设为单例来实现您想要的 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        

现在每当调用 MainWindow() 时,它总是 return 同一个实例。