简体   繁体   English

如何从隐藏控制台 python 脚本运行 python 控制台脚本?

[英]How run a python console script from a hiding console python script?

I'm not very experienced in python and I start building an app with Tkinter.我在 python 方面不是很有经验,我开始使用 Tkinter 构建应用程序。 Like it's a window project I open it with pythonw.exe but it call a console script and I can't display the console of the second script if the first is hidden... There is the piece of code which call the second script:就像它是一个 window 项目我用 pythonw.exe 打开它,但它调用控制台脚本,如果第一个脚本被隐藏,我无法显示第二个脚本的控制台......有一段代码调用第二个脚本:

from selenium_script import main    

self.btn_downloadAnime = tk.Button(self.frm_addAnime, text='Lunch download script with voiranime.com links',
                                       bg=self.colorBTN, font=22, activebackground=self.colorBG, height=2, width=50,
                                       command=main)

Is this what you are looking for:这是你想要的:

main.py : main.py

import tkinter as tk
import subprocess
import sys

COMMAND = "start python selenium_script.py"

def start_new_proc():
    proc = subprocess.Popen(COMMAND, close_fds=False, shell=True)

root = tk.Tk()
button = tk.Button(root, command=start_new_proc, text="Start new process")
button.pack()
root.mainloop()

selenium_script.py : selenium_script.py

user_input = input(">>> ")
print("You typed this: " + str(user_input))
input("Press enter to exit.")

I start a new process using subprocess.Popen .我使用subprocess.Popen开始一个新进程。 The new process starts with its own shell (because of the "start" in the COMMAND variable)新进程从它自己的 shell 开始(因为COMMAND变量中的"start"

Yes, I was missing the notion of process... The previous solution works and I found one too:是的,我错过了过程的概念......以前的解决方案有效,我也找到了一个:

def open_selenium(self):
        subprocess.Popen(args=['python', 'selenium2.py'], stdout=sys.stdout)

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

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