Python Forum
Adding timer on the Messagebox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding timer on the Messagebox
#1
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.
Reply
#2
See my post on adding a timer towards the bottom of this thread https://python-forum.io/Thread-Tkinter-K...ot-Aligned Note that the timer is independent of the widget being displayed.
Reply
#3
(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-K...ot-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.
Reply
#4
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?
Reply
#5
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()
Reply
#6
>@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.
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding the timer, smiley face, and flags in Minesweeper. javesike1262 5 3,121 May-04-2021, 10:20 PM
Last Post: deanhystad
  Tkinter messagebox and using datetime/timedelta Kaltex 3 3,245 Apr-07-2021, 06:23 PM
Last Post: Yoriz
  [Tkinter] Sound from the tkinter.messagebox tedarencn 2 5,825 Jul-11-2020, 10:45 AM
Last Post: steve_shambles
  [Tkinter] Messagebox with playsound khandelwalbhanu 6 4,306 May-16-2020, 11:40 AM
Last Post: chesschaser
  [Tkinter] messagebox is not being executed please help erwinsiuda 2 2,241 Apr-02-2020, 01:56 AM
Last Post: Larz60+
  [Tkinter] passing data to messagebox kmusawi 0 1,776 Feb-18-2019, 01:51 AM
Last Post: kmusawi
  [Tkinter] Button widget gets stuck from using tkinter.messagebox? Nwb 2 3,850 Jun-20-2018, 02:21 PM
Last Post: Nwb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020