Python Forum

Full Version: How to call a routine in another Python program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,
The following two programs, test31.py and test32.py have a pre_entry and post_entry routines.

# test31.py
def pre_entry(values):
    print("Pre Entry test31")
    for x in values:
        print(x)

def post_entry(values):
    print("Post Entry test31")
    for x in values:
        print(x)
# test32.py
def pre_entry(values):
    print("Pre Entry test32")
    for x in values:
        print(x)

def post_entry(values):
    print("Post Entry test32")
    for x in values:
        print(x)
In my main program, I want to prompt for which of the two programs to use. But I get a syntax error on the first def.
from tkinter import *
from tkinter import ttk

def main_program():
	python_lib = e1.get()

	from python_lib import *
	values = ["a","b","c"]
	pre_entry(values)
	values = ["d","e","f"]
	post_entry(values)
	print("Finished\n")

current_file = __file__
mw = Tk()
mw.geometry('700x300+400+200')
mw.title(current_file)

frame1 = Frame(mw)
framebot = Frame(mw)
frame1.pack(side=TOP,fill=X)
framebot.pack(side=BOTTOM,fill=X)

python_libs = ["test31","test32"]
w1 = Label(frame1, text="Template: ",font=("Times",16)).pack(side="left")
e1 = ttk.Combobox(frame1,width=40,font=("Times",16),values=python_libs)
e1.pack(side="left")

btn3 = Button(framebot,text='Go',font=("Times",16),command=main_program).pack(side="left")
btn4 = Button(framebot,text='Exit',font=("Times",16),command=mw.quit).pack(side="right")
mw.mainloop()
Any help would be appreciated.
You can only import * at the top level, not from inside a function or an if statement or a loop. This should have been mentioned in the error message. I don't think you should use "from x import *" anywhere.

Next time you post please include the error message and traceback information.