странное поведение времени обратного отсчета в python / tkinter - PullRequest
0 голосов
/ 21 декабря 2018
import tkinter as ttk

import random, time

def question1(*args):
        waitmsg= "You must wait for " + str(random.randrange(20,240,5)) + " minutes"
        ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
        countdown()
        # go do a bunch of stuff

def question2(*args):
    waitmsg= "WAIT " + str(root.wait_time) + " minutes"
    ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
        countdown()

def countdown(*args):
    # intended to display the remaining time as it counts down

    # the print works right away (no dealy!)
    print (root.wait_time)

    # the Label waits for a full 5 seconds then displays a 0 wait time!

    waitmsg= "WAIT count " + str(root.wait_time) + " minutes"
    ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
    if root.wait_time >= 1 :
        root.wait_time -= 1
        root.after(1000, countdown())
    else:     
        root.wait_time=5
    return


root = ttk.Tk()
root.title("Choose question!")
root.wait_time = 0

mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
if root.wait_time == 0:
##    root.wait_time= random.randrange(20,240,5)
    root.wait_time=5
ttk.Button(mainframe, text="question2?", command=question2).grid(column=4, row=1)
ttk.Button(mainframe, text="question1?", command=question1).grid(column=1, row=1)



root.bind('<Return>', question2)

root.mainloop()

1 Ответ

0 голосов
/ 21 декабря 2018

Основная проблема заключается в том, что вы используете результат обратного отсчета () в качестве функции обратного вызова в операторе root.after (1000, обратный отсчет ()).Вам нужно просто передать имя функции: root.after (1000, обратный отсчет).- acw1668

Похоже, это решает проблему.Спасибо!

Похоже, у вас есть много чего узнать о tkinter и stackoverflow: -)

...