Python 从主文件打开另一个tkinter程序

Python 从主文件打开另一个tkinter程序,python,tkinter,Python,Tkinter,我在我的主程序中尝试过这个 from tkinter import * import tkinter.filedialog import os root = Tk() def open(): PathPy = tkinter.filedialog.askopenfilename(title="Open a file",filetypes=[('PYTHON file','.py')]) os.system(PathPy) B = Button(root, text="Open a

我在我的主程序中尝试过这个

from tkinter import *
import tkinter.filedialog
import os
root = Tk()
def open():
    PathPy = tkinter.filedialog.askopenfilename(title="Open a file",filetypes=[('PYTHON file','.py')])
    os.system(PathPy)
B = Button(root, text="Open a file", command=open).pack()
root.mainloop()
在我的第二个文件中:

from tkinter import *
root2 = Tk()
root2.mainloop()
当我选择python文件时,什么都没有发生。。。您能告诉我如何解决这个问题吗?

在代码中添加两项可能会得到预期的结果

操作系统需要一个命令:

在子shell中使用字符串执行命令

比如python3myscript.py

但是,由于您使用的是自定义python编程环境,请将的python.exe路径传递到os.system:

其次,将最后一行root2.mainloop添加到第二个脚本中,以显示第二个Tk窗口


希望这有帮助

当您试图执行python文件时,请使用execfilefile。但是如果您只需要一个对话框,那么就使用顶级类


要保持两个窗口都运行,请使用不同的线程。用python很容易做到。

遗憾的是,当我将os.systemPathPy更改为os.system'python'+PathPy并在最后一行root2.mainloop添加时,什么都没有发生,但多亏了我的努力!嗯,对不起,我很抱歉,它不起作用,我以为它对你和对我都起作用。你用蟒蛇3吗?如何从命令行执行python?我在python2中测试了这个,因为我没有python3。是的,我使用python3是因为我在高中使用了一个特殊的软件。如果你能找到信息,这就是我必须使用的软件:听起来不错。请给出导入系统的输出;printsys.executable?C:\Program Files x86\EduPython\App\python.exe感谢您的帮助!:如果我犯了错误,我很抱歉,我的英语不是很好。
from tkinter import *
import tkinter.filedialog
import os
import sys

root = Tk()
pyexec = sys.executable

def open():
    PathPy = tkinter.filedialog.askopenfilename(title="Open a file",filetypes=[('PYTHON file','.py')])
    os.system('%s %s' % (pyexec, PathPy))

B = Button(root, text="Open a file", command=open).pack()
root.mainloop()