1

I've inherited this piece of code for a school project, and I need to attach an execute option for each of the choices.

Each of the options will execute another python script + parameters.

How can I add an "route.py -W" for the first option "route.py -Q" for the 2nd one and a "shutdown.py" for the 3rd one?

from Tkinter import *

def sel():    
    selection = "Component destination " + str(var.get())    
    label.config(text = selection)   

root = Tk()
var = IntVar()

R1 = Radiobutton(root, text="Send to warehouse", variable=var, value=1, command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(root, text="Send to QA", variable=var, value=2, command=sel)
R2.pack( anchor = W )

R3 = Radiobutton(root, text="Stop process", variable=var, value=3, command=sel)
R3.pack( anchor = W)

label = Label(root)
label.pack()
root.mainloop()
Josselin
  • 2,593
  • 2
  • 22
  • 35
  • the indentation is broken in your code. – Bryan Oakley Jun 19 '17 at 21:53
  • The code is working, indentation is broke just in the post. – MakavelliRo Jun 20 '17 at 09:08
  • We can't see your code, we can only see what's in your question. If you don't care enough to properly format your code, why would you expect people to care enough to answer? . – Bryan Oakley Jun 20 '17 at 11:17
  • @BryanOakley, some of the people here helped me and edited my format and indentation, what do you mean you can't see the code? Any tips on how to arrange it better? – MakavelliRo Jun 20 '17 at 16:54
  • I mean that we cannot see the code on your computer, we can only see the code that you post to a question. It is your responsibility to insure the code in the question is an accurate representation of your code. – Bryan Oakley Jun 20 '17 at 17:09
  • Ok, got it. Sorry, it's my first post and I'll try to be more careful with formatting the code next time. – MakavelliRo Jun 20 '17 at 17:48

1 Answers1

0

In your Radiobuttons callback function, you can check which button was just selected, and then run the desired command using an if/elif statement.

import os

def sel():
    selValue = var.get()
    if selValue == 1:
        os.system("route.py -W")
    elif selValue == 2:
        os.system("route.py -Q")
    elif selValue == 3:
        os.system("shutdown.py")

To launch a Python script with arguments, you can use os.system() as above. But a probably better way would be to rewrite that script so that it can be imported, and functions inside it can be called, as described here.

Josselin
  • 2,593
  • 2
  • 22
  • 35