简体   繁体   English

如何在Tkinter中执行Python程序

[英]How to execute a Python program in Tkinter

I have a simple Tkinter GUI, with one button and when the button is pushed I want it to run another program that I have written in Python. 我有一个简单的Tkinter GUI,带有一个按钮,当按下按钮时,我希望它运行我用Python编写的另一个程序。

 def openProgram ():
     #open up MyProgram.py

 MGui = Tk()
 MGui.geometry('450x450')

 mbutton = Button(text = "Go", command = openProgram).pack()

Seems easy enough, maybe I am not searching the correct terms. 似乎很容易,也许我没有搜索正确的术语。

You can call functions defined in another file by import ing that file. 您可以通过import另一个文件来调用该文件中定义的函数。

reticulator.py: reticulator.py:

def main():
    print "reticulating splines..."
    #do stuff here
    print "splines reticulated"

gui.py: gui.py:

from Tkinter import *
import reticulator

def openProgram():
    #call the `main` function defined in the other file
    reticulator.main()

MGui = Tk()
MGui.geometry('450x450')

mbutton = Button(text = "Go", command = openProgram).pack()
MGui.mainloop()

尝试使用os.system:

import os os.system("MyProgram.py")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM