-1

I am currently learning python through a book for beginners. I have some experience with java and c++. I would like to write a script so that when I log on to my profile or open a certain program a message will be displayed. For instance, if I am studying and open up Steam a message will display asking if that is what I should really be doing.

I have googled my question and have not been able to come up with any good results. Can someone point me in the right direction?

I am working on a mac if that information is useful

  • Can you elaborate on "a message will display" – Sishaar Rao Feb 18 '17 at 22:56
  • 2
    You could apply [this solution](http://stackoverflow.com/questions/1632234/list-running-processes-on-64-bit-windows). Have your program periodically poll the list of running processes every few seconds. If it sees "Steam", then it knows it should show a message box. – selbie Feb 18 '17 at 22:57
  • In response to Sishaar Rao, I am thinking of a simple text box that will pop up with a typed message – travelgravel Feb 18 '17 at 22:58
  • @selbie The solution you link to only works on ms-windows, while the OP is using a mac. – Roland Smith Feb 18 '17 at 22:59

1 Answers1

0

The following could be a quick and dirty start. It doesn't run very smoothly, because it doesn't make use of threading and callbacks, but for pointing you into one of many directions, it could help. One thing: python does not come with a fully featured core integrated gui library. You need to use third party apis like tk as I do in the sample. This requires python support for those tk libs on your OS. Make sure it is available by installing it. On debian linux it was

sudo apt-get install python3-tk

Also ensure psutil module is available for your python version. You can add it via pip install psutil. So here is the samlpe:

#!/usr/bin/python3

import psutil
from time import sleep
import tkinter as TK
from tkinter import messagebox

PROCNAME = "grep"

root = TK.Tk()
root.withdraw()


while 1:
  for proc in psutil.process_iter():
    if proc.name() == PROCNAME:
      messagebox.showinfo("process running", proc)
  sleep(0.1)

After storing the code in a file like "myprog.py", you then run the program from command line: python3 myprog.py The process name (PROCNAME variable) can be set individually. I was running grep -R * in another command line. If you do so, the message should pop up.

woodz
  • 737
  • 6
  • 13