I'm actually helping my wife. She's learning Python. Me? VB.net and C# are my preferance. So I try to help her.

All she wants to do is paint a command button, use this to launch another program written in python.

I have spent the past few days trying to find something and am coming-up dry. In Visual Studio, I'm fine. Here...well...nothing's documented too well. Her textbooks are useless.

I can only surmise that I use the command= to call some defined function somewhere upstream in the code, previously declared, and then somehow this function "calls" the python code.

I don't see any syntax to help resolve:
the actual button syntax (I can get a button click to terminate the program, not useful but a start).
whether the lambda function should be used (doubtful).
How to correctly path to the piece of Python code (or script).
How to syntactically "call" the code.

At this point, I'm not too impressed with Python, but I still hate to leave her guessing. Even her teacher, also somewhat new to Python, is stumped.

Really, it shouldn't be this hard to do something so intuitive elsewhere (Like Visual Studio and C#).

So, would someone care to throw this old dog a bone of "working code" that creates a button, does whatever needs to be done to call a snippet of python (compiled, presume compiler is also on the running machine).

Thanks to anyone who can help!

Here we go ...

''' tk_button_run_hello.py
run an external Python program via a tkinter button
tested with Python3
'''

# replace tkinter with Tkinter in Python2
import tkinter as tk

def run_hello():
    execfile("hello.py")

root = tk.Tk()
btn = tk.Button(root, text="Run program Hello", command=run_hello)
btn.pack()
root.mainloop()

Here is hello.py ...

''' hello.py
simple test program
'''

for n in range(5):
    print("Hello")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.