Секундомер python Код Tkinter не открывается во всплывающем окне Flask. Он работает нормально, если запущен только скрипт python. На консоли не выдано никаких ошибок - PullRequest
1 голос
/ 25 апреля 2020

Если запущен только скрипт StopWatch.py, он отлично выдаст требуемый вывод.

Однако, через flask, когда url: http://127.0.0.1: 5000 / , открывает python script Stopwatch.py ​​не показывает таймер по мере необходимости.

Я провел почти весь день, но не смог получить требуемый вывод через flask app

Вот код:

app.py

from flask import Flask, render_template, request
import subprocess

activity = ""
attuid = ""
duration = 0

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST' :
        activity = request.form.get('activity')
        attuid = request.form.get('attuid')
        cmd = 'StopWatch.py'
        duration = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        print(activity, attuid, duration)
    return render_template('index.html')

index. html

<html>
<head>
    <title>Leads Activity Tracker Automation</title>
</head>
<body>
    <form method="POST" action="/">
        <h3>Select an activity</h3>
        <input type="radio" value="TROUBLESHOOTING_CPDO_ISSUES" name="activity">TROUBLESHOOTING_CPDO_ISSUES<br>
        <input type="radio" value="MAJOR_EVENT_HEALTH_CHECKS" name="activity">MAJOR_EVENT_HEALTH_CHECKS<br>
        <input type="radio" value="TRAINING_PEERS" name="activity">TRAINING_PEERS<br>
        <input type="radio" value="UPDATE_JOB_AID_AND_PROCESSES" name="activity">UPDATE_JOB_AID_AND_PROCESSES<br><br>
        <input type="text" value="attuid" name="attuid">
        <input type="submit">
    </form>

StopWatch.py ​​

import tkinter as Tkinter
import time


counter = -1
initial=0
duration=0
running = False
def counter_label(label): 
    def count(): 
        if running: 
            global counter 

            # To manage the intial delay. 
            if counter==-1:             
                display="Starting..."
            else: 
                display=str(counter) 

            label['text']=display   # Or label.config(text=display) 

            # label.after(arg1, arg2) delays by  
            # first argument given in milliseconds 
            # and then calls the function given as second argument. 
            # Generally like here we need to call the  
            # function in which it is present repeatedly. 
            # Delays by 1000ms=1 seconds and call count again. 
            label.after(1000, count)  
            counter += 1

    # Triggering the start of the counter. 
    count()      

# start function of the stopwatch 
def Start(label): 
    global running
    global initial
    running=True
    counter_label(label) 
    start['state']='disabled'
    stop['state']='normal'
    initial = time.time()
    return initial

# Stop function of the stopwatch 
def Stop(): 
    global running
    global final
    global duration
    start['state']='disabled'
    stop['state']='disabled'
    running = False
    final = time.time()
    duration = round(final - initial,2)
    label['text'] = (" Total duration of activity is :", duration, "seconds"  )
    return duration


root = Tkinter.Tk() 
root.title("Stopwatch") 

# Fixing the window size. 
root.minsize(width=250, height=70) 
label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold") 
label.pack() 
start = Tkinter.Button(root, text='Start',  
width=15, command=lambda:Start(label)) 
stop = Tkinter.Button(root, text='Stop',  
width=15, state='disabled', command=Stop) 
start.pack() 
stop.pack()
...