简体   繁体   English

我如何使用子进程在 python tkinter 中传递文件名

[英]how can i pass a file name in python tkinter using subprocess

below is my code this is a mini IDE for a class project I am stuck here am trying to build an IDE that compiles java. below is my code this is a mini IDE for a class project I am stuck here am trying to build an IDE that compiles java. I downloaded JDK and am using subprocess to pipe cmd and communicate with javac but I need to pass the file name with extension so it just shows output and I also need help with outputting a console because it tends to only open in visual studio terminal please help me because I will be submitting on Thursday.我下载了 JDK 并正在使用 pipe cmd 的子进程并与 javac 通信,但我需要传递带有扩展名的文件名,所以它只显示 output,因为它倾向于在可视化终端中打开帮助,输出控制台也需要帮助我,因为我将在星期四提交。 femi.femiii@gmail.com femi.femiii@gmail.com

from tkinter import filedialog
from tkinter import messagebox
import subprocess
import os

name_file = os.path.basename(__file__)


    # run button that opens command line
    def run(self, *args):

        p1 = subprocess.Popen('cmd', shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p2 = subprocess.Popen('javac name_file; java name_file', shell=True, stdin=p1.stdout)
        p1.stdout.close()
        out, err = p2.communicate()


if __name__ == "__main__":
    master = tk.Tk()
    pt = PyText(master)
    master.mainloop()```

The easiest way I can think of would be to use sys argv's to pass the file name to a node or java routine我能想到的最简单的方法是使用 sys argv 将文件名传递给节点或 java 例程

modify your python script to pass info to command line in argv修改您的 python 脚本以将信息传递到 argv 中的命令行

fname = file 
#file you want to pass 
pythonCall = 'node javascript.js -- '+fname
#the -- passes what follows into the sys argvs
#this is python notation, java/node may not need the '--' but just a space
runCommand = 'cmd.exe /c ' + pythonCall
subprocess.Popen(runCommand)

then at the start of your javascript, this will give you the file name that was passed into argv, its from the link I included below.然后在 javascript 的开头,这将为您提供传递给 argv 的文件名,它来自我在下面包含的链接。

var args = process.argv.slice(2);

if you need more help accessing process.argv in java:如果您需要更多帮助来访问 java 中的 process.argv:

How do I pass command line arguments to a Node.js program? 如何将命令行 arguments 传递给 Node.js 程序?

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

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