Python Forum
Adding timer on the Messagebox - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Adding timer on the Messagebox (/thread-16030.html)



Adding timer on the Messagebox - aniyanetworks - Feb-11-2019

Hello Experts,
I would like to show some message on the Message Box, with a Timer on it. Messagebox will have 2 min countdown clock and after 2 min it will be close.

My code is here:
from tkinter import *
from tkinter import messagebox
window= Tk()

def wifiOnscript():
    messagebox.showinfo("Information!!", "This process may take up to 2 min. Please try after 2min...")

wifiOnButton=Button(window,text="WiFi-ON",command=wifiOnscript, height=1, width=22)
wifiOnButton.grid(row=2,column=0,padx=40, pady=10)

window.mainloop()
Please let me know how can I add the clock to my message box.
Thanks in advance.


RE: Adding timer on the Messagebox - woooee - Feb-11-2019

See my post on adding a timer towards the bottom of this thread https://python-forum.io/Thread-Tkinter-Keypad-Not-Aligned Note that the timer is independent of the widget being displayed.


RE: Adding timer on the Messagebox - aniyanetworks - Feb-12-2019

(Feb-11-2019, 09:48 PM)woooee Wrote: See my post on adding a timer towards the bottom of this thread https://python-forum.io/Thread-Tkinter-Keypad-Not-Aligned Note that the timer is independent of the widget being displayed.


Hello Woooee,
I am a newbie in Python and your scripts look a like science language to me, I am not getting anything sorry.


RE: Adding timer on the Messagebox - woooee - Feb-12-2019

That is the only way to do it that I know of. "I am not getting anything" is too vague for any response. What have you tried and what do you understand and what do you not understand?


RE: Adding timer on the Messagebox - wuf - Feb-13-2019

Hi aniyanetworks

@woooee: I think you have placed a wrong link:
https://python-forum.io/Thread-Tkinter-K...ot-Aligned
This link has nothing to do with a count down clock.

@aniyanetworks: Here i have placed a possible variant script of a count down box. Build your own messagebox to display the count down timer. If you intend to build own GUI's you must have a closer look at Classes! To replace the ugly bitmap icon in my script pleace import your own icon image:
import tkinter as tk

APP_TITLE = "Count Down Box"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 350
APP_HEIGHT = 200


class CountDownMessageBox(tk.Toplevel):
    TEXT_FONT = ('Helevtica', 12, 'bold')
    TEXT = "This process may take up to 2 min. Please try after 2min..!"
    TIMER_FONT = ('Helevtica', 16, 'bold')
    TIMER_COUNT = 10 # Seconds
    
    def __init__(self, app, msg_text=TEXT):
        self.app = app
        self.msg_text = msg_text
        
        tk.Toplevel.__init__(self, app.main_win)
        
        self.build()
        
    def build(self):
        main_frame = tk.Frame(self)
        main_frame.pack(expand=True, padx=20, pady=20)
        
        message_var = tk.StringVar(self.app.main_win, self.msg_text)
        tk.Label(main_frame, bitmap='hourglass', padx=10, font=self.TEXT_FONT,
            compound='left', textvariable=message_var, wraplength=200,
            fg='gray40').grid(row=0, column=0)
        
        self.timer_var = tk.StringVar()
        tk.Label(main_frame, textvariable=self.timer_var, font=self.TIMER_FONT,
            fg='blue').grid(row=1, column=0, padx=20, pady=20)
        
        self.count_down()
        
    def count_down(self, time_count=TIMER_COUNT):
        self.timer_var.set("{} Seconds".format(time_count))
        if time_count == 0:
            self.destroy()
            self.app.count_down_callback()
        time_count -= 1
        self.after(1000, self.count_down, time_count)
        
                    
class Application:

    def __init__(self, main_win):
        self.main_win = main_win
        
        self.count_down = False
        self.build()
        
    def build(self):
        self.main_frame = tk.Frame(self.main_win)
        self.main_frame.pack(fill='both', expand=True)

        wifiOnButton = tk.Button(self.main_win, text="WiFi-ON",
            command=self.wifiOnscript, height=1, width=22)
        wifiOnButton.pack(expand=True, padx=40, pady=10)

    def wifiOnscript(self):
        if not self.count_down:
            CountDownMessageBox(self)

    def count_down_callback(self):
        print("Count down finished!")
        
        
def main():
    main_win = tk.Tk()
    main_win.title(APP_TITLE)
    main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))

    app = Application(main_win)
    
    main_win.mainloop()
 
 
if __name__ == '__main__':
    main()



RE: Adding timer on the Messagebox - woooee - Feb-13-2019

>@woooee: I think you have placed a wrong link:
https://python-forum.io/Thread-Tkinter-K...ot-Aligned
This link has nothing to do with a count down clock.

True as the link you posted goes nowhere. The link that I posted above is correct and the code at that location can be run and shows a countdown timer.


RE: Adding timer on the Messagebox - aniyanetworks - Feb-13-2019

(Feb-13-2019, 07:39 PM)woooee Wrote: >@woooee: I think you have placed a wrong link:
https://python-forum.io/Thread-Tkinter-K...ot-Aligned
This link has nothing to do with a count down clock.

True as the link you posted goes nowhere. The link that I posted above is correct and the code at that location can be run and shows a countdown timer.

Thanks, guys for your kind time and help.