Execute terminal commands using python gtk - terminal

I want to make a quick app using gtk and python that when a button is clicked it executes a terminal command. Anyway I can achieve this?

You can follow this tutorial for Python and Gtk. The second example in the tutorial shows how to create a window with a button so to run a command you just need to change the on_button_clicked callback. You can use either os.system or the subprocess module to run commands with Python.
So to run ls for example:
import subprocess
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
subprocess.run(["ls"])
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Related

How can I activate Conda environment using python code?

How can I activate a Conda environment using python code?
Here are things I tried so far but they don't seem to change my environment:
import os
os.system('conda activate envname')
# Doesn't work but returns no errors.
import os
stream = os.popen('conda activate envname')
output = stream.read()
# Doesn't work but returns no errors.
import subprocess
process = subprocess.Popen(['conda', 'activate', 'envname'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
stdout, stderr = process.communicate()
# Doesn't work but returns no errors.
So how can I change my Conda environment using python, and I want it to work on all platforms (linux, mac, windows)?
EDIT 1:
So from this question it seems that all I'm doing is changing the environment temporarily during the existence of the subprocess. I want a way to change it in my current running shell...

Python 3.5 os.execl not working as it should on Windows 10

I would like to restart my python script from within itself. So I wrote a small test script:
def restart_program():
print ("Restarting")
python = sys.executable
os.execl(python, python, * sys.argv)
if __name__ == "__main__":
if answer.lower().strip() in "y yes".split():
restart_program()
So, in theory, I suppose to get indefinite "Restarting" to be printed in the console. And it is exactly what happens - except with Python 3.5 on Windows 10.
When switching to Python 3.5 on Windows 10 - it sucks. I need to press enter twice in order to get next "Restarting". And in more complicated script it crashes.
I also tried MacOS - It works fine on both Python 2.7 and Python 3.5
Can someone suggest a correct way to do it on Windows 10?

Using Bash to run 2 python scripts at the same time

I want to run two Python Scripts at the same time. I found that using Bash you can do that. So i wrote the next code
#! /usr/bin/env bash
import camera_centroid
import testsss
python camera_centroid.py &
python testsss.py &
When i run it i get a SyntaxError: invalid syntax
Why?
Looks like you have mixed between python and bash,
you don't need the import in the bash script.
#!/usr/bin/env bash
python camera_centroid.py &
python testsss.py &
wait # wait for jobs to be done
make sure you adding execute permissions to the scripts
chmod +x testsss.py camera_centroid.py
and finally run the script ./your_file.sh
When you write:
import foo
in a shell script, you are not importing the python module, instead, you are calling the $(which import) command/alias/function.
For example, if you have ImageMgick installed, very likely, you are making screenshot for window(s).
If you want to import python module, those import foo should be in your python files.

Enthought Canopy - passing sys.argv from PySide Qt program

I've recently been looking at the Enthought distro of iPython. Today I decided to see if I could get some Qt GUI progs running and was successful after making minor changes. Simple example:
import sys
from PySide import QtGui # was 'from PyQT4 import QtGui'
# app = QtGui.QApplication(sys.argv) -- not needed
win = QtGui.QWidget()
win.resize(320, 240)
win.setWindowTitle("Hello MIT 6X!")
win.show()
sys.exit() # was 'sys.exit(app.exec_())'
But I would like to be able to pass sys.argv in some cases. Most example code I see is in the form of the commented out 'app = ' line above. If I include it, I get
'RuntimeError: A QApplication instance already exists.'
Suggestions for passing arguments appreciated.
Two separate issues:
1) Passing command line arguments: As you have probably noticed, when you do the "Run" command from the Canopy editor, all it does is issue the IPython %run magic command. You can type the same command in the IPython shell, plus command line parameters, which your program will see. Or to save keystrokes, do this auto-generated Run command once, then press Up Arrow in the IPython shell to recall that auto-generated %run command, then enter your parameters after the filename, and then press Enter. You'll end up with an IPython magic command like this:
%run pathtoprog/myprogrampy p1 p2 p3
We (Enthought) are considering adding a setting for command-line parameters so that you could do "Run with parameters" and have the best of both worlds.
2) Existing QApplication: By default, Canopy's IPython is running in IPython's interactive Pylab mode, with a Qt backend. If you don't want this, you can just disable Pylab mode in the Canopy Preferences/Python menu, or change the Pylab mode to Inline (for matplotlib) instead of Interactive.
For maximum flexibility, with a bit more work, you could (as matplotlib does) introduce logic which checks whether a QApplication already exists, use it if it exists and create it if it does not.

Problem with OS X automator and python and os.system

If I use
import os
os.system("pdflatex myfile.tex")
from iron python it just works. If I start to Automator and use "Run shell script" switch the shell to python the same command is not executed anymore. Any ideas ?

Resources