1

I am trying to use four diferents python scripts in a GUI application.

Every script has around 500 code lines. Then I don´t like to include the every full scrip as a function.

This is the skeleton of the application:

from FullConversor import *   #this is the .py gui  
import sys    
import datetime
import os
import pandas as pd
import shapefile as shp  
import csv  
import tkinter.filedialog


class FullConversorGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonConvert, QtCore.SIGNAL ('clicked()') ,self.conversor)
        QtCore.QObject.connect(self.ui.pushButtonClose, QtCore.SIGNAL ('clicked()') ,self.close)


    def conversor(self):
        if self.ui.radioButton1.isChecked()== True: 
            pass
        if self.ui.radioButton2.isChecked()== True:  
            pass
        if self.ui.radioButton3.isChecked()== True:  
            pass
        if self.ui.radioButton4.isChecked()== True: 
            pass


if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    myapp = FullConversorGUI()
    myapp.show()
    sys.exit(app.exec_())

Every radioButton must launch a python script, just a .py file who runs fine alone.

How do I do that?

peterh
  • 11,875
  • 18
  • 85
  • 108
kamome
  • 828
  • 3
  • 11
  • 27
  • Seriously consider not comparing the return value of a function named `isChecked()` with a Boolean literal. – unwind May 21 '15 at 15:08

1 Answers1

0

Call import on your script, and run the main function of it when you need.

Pretend you have a script called myscript.py

# this is myscript.py

def main():
    # this function will be called from your gui. all your logic should be here.
    print "my script"

if __name__ == '__main__':
    # this function is called when you invoke the script from the command line. 
    main()

Then in your gui....

import myscript

if self.ui.radioButton1.isChecked()== True: 
    myscript.main()
    # continue after the script runs

This would result in the following output to stdout:

my script

Obviously, you wouldn't have just a print statement in myscript.py. You can have it run whatever logic you would like.

Gil
  • 370
  • 2
  • 11
  • if self.ui.radioButton1.isChecked()== True: myscript.main() – kamome May 21 '15 at 14:53
  • 1
    edited to reflect users specific case. Thanks @kamome – Gil May 21 '15 at 14:55
  • Well I can´t understand that. I include if __name__ == "__main__": main at the end of my script but when I import that, the script runs and I can not fix it to a radio button – kamome May 22 '15 at 21:07
  • @kamome your python script is simply a file. In this case, let's pretend it is called `myscript.py`. If your script is in the same directory as where you are running your gui, you can import it just the same way as you would import a standard library in python (which you have done at the top of your code with `import sys` etc.) Then you are going to call the execution of your script's main method to run it... Please let me know what was unclear in more detail and I'll explain the answer further. – Gil May 22 '15 at 21:11
  • @kamome a method called `main()` is different from `if __name__ == '__main__'` you can have both in your script. I'll edit my post again to see if you get it. – Gil May 22 '15 at 21:18
  • @kamome see [here](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) for a full explanation. – Gil May 22 '15 at 21:22
  • Well. My script is a 400 code lines -----example.py No functions in the script But when I import the example.py at the begining of the pyw script, the example.py runs....but I´d like to run when the radioButton is on – kamome May 22 '15 at 21:27
  • @kamome so change your script and put everything in one main function... It shouldn't affect the functionality of your code whatsoever. – Gil May 22 '15 at 21:30
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78579/discussion-between-kamome-and-gil). – kamome May 22 '15 at 21:34