Python Forum
How to call a routine in another Python program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call a routine in another Python program
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,570 May-04-2021, 08:58 PM
Last Post: liudr
  A dynamic link library (DLL) initialization routine failed ish93 0 1,752 Jan-11-2021, 08:22 PM
Last Post: ish93
  The logic of a main routine john7 6 3,124 Mar-19-2020, 06:10 AM
Last Post: john7
  keyboad scanner routine(linux and windows) 1885 0 1,881 Oct-26-2019, 03:34 PM
Last Post: 1885
  Call pip3 from python folder build by me call pip3 from the /usr/bin Suryavarman 3 3,630 Oct-07-2019, 10:23 PM
Last Post: Suryavarman
  Embedding Args in external program call brizflysdrones 5 4,840 May-09-2017, 08:29 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020