-1

How to display only message box and hide the tk modal window? It would be great if someone can modify this below.

import time
import Tkinter as tk
import tkMessageBox

root = tk.Tk()
root.withdraw

def timer(hours):
    seconds = hours * 60
    start = time.time()

    elapsed = 0
    while elapsed < seconds:

        time.sleep(0.0010)
        elapsed = time.time() - start

    elapsed = elapsed//60

    tkMessageBox.showinfo("Done", "Done Today" +str(elapsed) + " Hrs")

timer(1)
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
Jake
  • 65
  • 2
  • 14
  • As a note, avoid using `time.sleep()` in a gui program- it will prevent the gui from updating, meaning it can't display each tick as the Tcl loop won't get control. Instead, have your function call itself `after` a set period of time via a `root.after([milliseconds],[function])` call. This will allow Tcl/Tk to update the gui and also won't freeze up everything (i.e. with `sleep` your buttons won't do anything until the mainloop gets control) – Delioth Jul 07 '16 at 15:35

2 Answers2

0

well you have almost did it...

root = tk.Tk()
root.withdraw()
timer(.1)
root.mainloop()
Eular
  • 1,707
  • 4
  • 26
  • 50
  • this kills my actual sleep, if i am setting up for 1 minute it takes more than minutes.. – Jake Jul 08 '16 at 06:25
0

You can use this method.

import time
import Tkinter as tk

root = tk.Tk()
root.withdraw

def timer(hours):
    seconds = hours * 60
    start = time.time()

    elapsed = 0
    while elapsed < seconds:

        time.sleep(0.0010)
        elapsed = time.time() - start

    elapsed = elapsed//60

    #tkMessageBox.showinfo("Done", "Done Today" +str(elapsed) + " Hrs")
    root.title("Done")
    label = tk.Label(root, text= "Done Today " +str(elapsed) + " Hrs" )
    label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
    button = tk.Button(root, text="OK", command=lambda: root.destroy())
    button.pack(side="bottom", fill="none", expand=True)
    root.mainloop()

timer(1)
dazzieta
  • 662
  • 4
  • 20
  • Earlier i had used this, found in different post here. But it looks odd with minimize,expand and close icons on corner of dialog. – Jake Jul 08 '16 at 06:20