如何在Tkin中执行Python程序

2024-04-23 12:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的Tkinter图形用户界面,只有一个按钮,当按下按钮时,我希望它运行我用Python编写的另一个程序。

 def openProgram ():
     #open up MyProgram.py

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

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

似乎很容易,也许我没有找到正确的术语。


Tags: py程序tkinterdefbuttonopen图形用户界面按钮
2条回答

尝试使用os.system:

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

您可以通过import调用另一个文件中定义的函数。

网状肌.py:

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

图形用户界面:

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()

相关问题 更多 >