从主文件打开另一个 tkinter 程序 [英] Open another tkinter program from a main file
问题描述
我已经在我的主程序中尝试过
I have try this in my main program
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()
就在我的第二个文件中:
and just this in my second file:
from tkinter import *
root2 = Tk()
root2.mainloop()
当我选择 python 文件时,什么也没有发生......你能告诉我如何解决这个问题吗?
When I choose the python file, nothing happens... Can you please, tell me how to fix this ?
推荐答案
向您的代码添加两个内容可能会得到预期的结果.
os.system 需要一个命令"操作系统:
os.system expects a "command" for the operating system:
在子shell中执行命令(一个字符串).
Execute the command (a string) in a subshell.
像python3 myscript.py
.
但是,由于您使用的是自定义 Python 编程环境,因此请传递 EduPython 的路径python.exe
到 os.system
:
However, since you are using a custom python programming environment, pass the path of the EduPython's python.exe
to os.system
:
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()
其次,将最后一行 root2.mainloop()
添加到您的第二个脚本中,使第二个 Tk 窗口出现.
Second, add the last line root2.mainloop()
to your second script to make the second Tk window appear.
希望这会有所帮助.
这篇关于从主文件打开另一个 tkinter 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!