2015-04-28 170 views
0

这是我main.py我有一个函数调用loadShot,我想从另一个如何调用pyside主窗口的功能从另一个python脚本

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类中调用该函数而不加载另一个实例。

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

这不是100%清楚你要问什么,但它听起来像你想调用一个绑定的方法(一个方法,有'self'作为第一个参数,在你的情况下'loadShot'),没有实例化你的类。你不能那样做。你可以使该方法成为一个类方法,但是你会失去对像'self.shotName_Line.text()'等实例变量的引用。你能扩展你实际上想要实现的一点吗? – 101

+0

感谢您的回复。我有我的main.py,其中有我的pior gui MainWindow。我有几个与gui交互的方法。 loadShot()从一行编辑信息并将其写入文件。在我的other.py文件中。我有一个键盘监听器,它具有handle_CTRL_L()方法,我希望能够调用loadShot()方法。这不可能是一个实例,因为它重新启动了我的GUI。杀死所有信息并且不向文件写任何东西。我希望能够在调用方法时发出的handle_ctrl_L()方法下使用自定义信号。谢谢。 –

回答

0

在一个项目中,我必须启用一个模块对我的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 

第二个模块(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) 
0

如果你的应用程序只需要MainWindow的一个实例,那么你可以通过使它成为单例类来实现你想要的:

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()时,它将始终返回相同的实例。

+0

PS:为什么你在使用'Tkinter'文件对话框? [QFileDialog']有什么问题(http://doc.qt.io/qt-4.8/qfiledialog.html)? – ekhumoro

+0

是的,我知道这是来自测试的旧代码。我已经将它交给了QFILEDIALOG。谢谢 –

相关问题