gpt4 book ai didi

python - 按下按钮后同时运行 2 个 python 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:10 25 4
gpt4 key购买 nike

我想在按下按钮后同时运行 2 个 python 脚本(1.py 和 2.py)。我怎样才能做到这一点?

1.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port = 5000)

2.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Nice to meet you'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port = 5050)

按钮.py

from Tkinter import *
root = Tk()
def Start():
#run 1.py and 2.py

Button_Start = Button(root, text = "Start", command = Start)
Button_Start.pack(side = LEFT)
root.mainloop()

最佳答案

您可以使用线程来完成此操作,并且所有文件都应该位于同一目录中。

1. first_flask.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello'

def flaskThread1():
app.run(debug=True, host='0.0.0.0', port = 5000)

2. second_flask.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Nice to meet you'

def flaskThread2():
app.run(debug=True, host='0.0.0.0', port = 5050)

3.在按钮运行脚本中,

import thread
from first_flask import flaskThread1
from second_flask import flaskThread2

#At first button pressed
thread.start_new_thread(flaskThread1,())
thread.start_new_thread(flaskThread2,())

关于python - 按下按钮后同时运行 2 个 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com