我想在按下按钮后同时运行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)
Button.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()
答案 0 :(得分:1)
您可以使用thread
执行此操作,并且所有文件都应位于同一目录中。
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,())