2017-08-17 53 views
0

我有一个组织设置问题,我需要帮助。这是我目前的文件夹结构。使用GUI加载Python模块

enter image description here

我想要做什么与包含功能指定AppCommands模块运行主UI。根据我想要运行该工具的应用程序。有没有办法使用另一个python文件,我可以加载gui和相关的应用程序命令moduel?所以当用户点击按钮时,它会调用更正应用程序命令。

所以说,比如我创建这样的伪代码

主要执行PY文件为Photoshop

Import photoshop.appcommands as cmds 
Import GUI 
Gui(cmds) 

我怎么然后告诉我主要的GUI工具加载的Photoshop模块Python文件'AppCommands'何时运行?

应用#1代码:

def runTool(): 
    msg = 'This is Notepad' 
    print msg 

应用#2代码:

def runTool(): 
    msg = 'This is Photoshop' 
    print msg 

主UI代码:

import sys 
import os 
from PySide import QtGui, QtCore 
import AppCommands as cmds 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self,parent=None): 
     super(MainWindow, self).__init__(parent) 

     self.uiButton = QtGui.QPushButton('Button', self) 

     # layout 
     grid = QtGui.QGridLayout() 
     grid.addWidget(self.uiButton, 3, 1) 
     main_widget = QtGui.QWidget() 
     main_widget.setLayout(grid) 
     self.setCentralWidget(main_widget) 

     self.uiButton.clicked.connect(self.browse_clicked) 

    # actions 
    def browse_clicked(self): 
     print 'Execute Command' 
     cmds.runTool() 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    ex = MainWindow() 
    ex.show() 
    sys.exit(app.exec_()) 
+0

变化'进口AppCommands为cmds'为'进口{} XXXXX作为.AppCommands cmds' – eyllanesc

+0

这是一个好主意,我怎么然后运行与指定模块的工具? – JokerMartini

+0

你如何确定指定的模块? –

回答

0

嗯,如果我UND理解正确,你需要选择正确的模块来加载一个函数,然后你用一些变量参数启动程序?

在这种情况下,您可以同时导入并尝试全局:

import notepad.AppCommands 
import photoshop.AppCommands 

... 

# in your app init code: 

x = 'photoshop' # an arg passed from config/CLI or from somewhere else 
actual_module = globals()[x] 
actual_module.AppComands.runTools() 

++但如果你只是不知道如何从某一模块运行某些功能,并关注@ eyllanesc的答案。