Python Forum
[Tkinter] notify after x number of days.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] notify after x number of days.
#1
hey can someone plz help write a tkinter python3 code which show notification after x number of days where x will be list of dates to be notified .how do i loop through that list and continusly check if the days has ended.Plz help
Reply
#2
You could perhaps use a specialized module such as schedule
Reply
#3
take a look at 'after' which creates an event after an elapsed amount of time, in a separate thread, so
processing can continue as normal until the event. Can be threaded for multiple events.
Here's one I wrote several years ago:

prog1 EventTimer.py:
import threading


class EventTimer:
    '''
    Creates overlaping threaded timed events
    Events can be set to trigger in days, hours, minutes, and/or seconds
    Any number of events can be created, and are treated independently, thus can
    overlap each other.
    Program execution will continue until all events have completed.
    the function that is passed to each timer will execute after the timeout has
    been reached
    '''

    def __init__(self):
        self.threadList = []

    def addEvent(self, func, days=0, hours=0, minutes=0, seconds=0):
        times = [days, hours, minutes, seconds]
        multiplier = [86400, 3600, 60, 1]
        count = 0

        for n in range(len(times)):
            count += times[n] * multiplier[n]

        t = threading.Timer(count, func)
        self.threadList.append(t)

    def startAll(self):
        # Start threads
        [th.start() for th in self.threadList]

    def waitForFinish(self):
        # wait for all to finish
        [th.join() for th in self.threadList]
Prog2 tryit.py
from EventTimer import *

def function1():
    print('Function 1 triggered')

def function2():
    print('Function 2 triggered')

def function3():
    print('Function 2 triggered')

def finalone():
    print('Final function triggered')

def launch():
    e = EventTimer()
    e.addEvent(function1, seconds=5)
    e.addEvent(function2, seconds=10)
    e.addEvent(function3, seconds=20)
    e.addEvent(finalone, seconds=25)
    e.startAll()
    print("This statement shows that you can do other things while")
    print('Waiting for events to trigger')
    print('And then wait for all to finish')
    print('Events will still trigger even if other processes are running')
    print('Only call waitForFinish when you are ready to do so')
    e.waitForFinish()

if __name__ == '__main__':
    launch()
Reply
#4
#Lazr60+ thanks man for the help but actually the program has to wait for some days where the program could be stoped in between the intervals of days ...For suppose if I have to run fun1 after 7 days and the program will be closed 1, 2 times and again started and will the program still run fun1? plz

from tkinter import *
from datetime import datetime, timedelta 
import pickle
from tkinter import messagebox

filename = "file.pk" #filename data stored as dic = {time:[name,item]}

root = Tk()

t = IntVar()
i = StringVar()
n = StringVar()

 
def Notification_On_Completing(n, i, t):
	return messagebox.showinfo("Completed", "Name:- {}, Item:-{}, in Days:-{}".format(n, i, t)) 


def Check_If_Time_Is_Over():   #my actual problem is in this function 
	 
	with open(filename, "rb") as f: 
		dic = pickle.load(f)
	 
	now = datetime.now()

	for i, j in dic: #trying to loop and check if the time == now() 
	        if i == now: 
	            Notification_On_Completing(j[0], j[1], i) #if its true return the key which is equal with its value

	        elif i =! now: #if now time i am tryinng to show how much time left
	        	print(i - now, "Time has left for name:-{}, item:-{}".format(j[0],j[1]))
	        else:
	        	root.after(10000, Check_If_Time_Is_Over)

def SaveTheDaysToNotify():

	now = datetime.now()
	time = t.get()  # days to wait beforer notifying
	item = i.get()  #item name    
	name = i.get()  #name 

	end = now + timedelta(days=time)  #adding today with the number of days to notify
	
	with open(filename.pk, "rb") as f: # avoiding the overide of the files
		dic = pickle.load(f)
	  
	dic= {end:[name, item]}  # saving a days to notify as dic which will also show the name , and item
	with open("file.pk", "wb") as f: #keeping record of the time time to notify
		pickle.dump(dic, f)
	  
    Check_If_Time_Is_Over()


#Gui starts from here

time1 = Entry(root,textvariable=t).pack()  #taking entry as of time, name and item
name1 = Entry(root,textvariable=n).pack()
item1 = Entry(root, textvariable=i).pack()

ss = Button(root,text="done",command=clicked).pack() #adding to the pickle database with format as dictionary as dic ={time:[name, item]}

root.mainloop()
Check_If_Time_Is_Over()

if __name__ == "__main__":
	main_()
this is what i tried......plz i need help
Reply
#5
The apscheduler library implements jobs that survive scheduler restart by storing jobs in a database (such as an sqlite database).
Reply
#6
from tkinter import *
from datetime import datetime, timedelta 
import pickle
from tkinter import messagebox

filename = "file.pk" #filename data stored as dic = {time:[name,item]}

root = Tk()

t = IntVar()
i = StringVar()
n = StringVar()

 
def Notification_On_Completing(n, i, t):
	return messagebox.showinfo("Completed", "Name:- {}, Item:-{}, in Days:-{}".format(n, i, t)) 


def Check_If_Time_Is_Over():   #my actual problem is in this function 
	 
	with open(filename, "rb") as f: 
		dic = pickle.load(f)
	 
	now = datetime.now()

	for i, j in dic: #trying to loop and check if the time == now() 
	        if i == now: 
	            Notification_On_Completing(j[0], j[1], i) #if its true return the key which is equal with its value

	        elif i not in now: #if now time i am tryinng to show how much time left
	        	print(i - now, "Time has left for name:-{}, item:-{}".format(j[0],j[1]))
	        else:
	        	root.after(10000, Check_If_Time_Is_Over)

def SaveTheDaysToNotify():

	now = datetime.now()
	time = t.get()  # days to wait beforer notifying
	item = i.get()  #item name    
	name = i.get()  #name 

	end = now + timedelta(days=time)  #adding today with the number of days to notify
	
	with open(filename.pk, "rb") as f: # avoiding the overide of the files
		dic = pickle.load(f)
	  
	dic= {end:[name, item]}  # saving a days to notify as dic which will also show the name , and item
	with open("file.pk", "wb") as f: #keeping record of the time time to notify
		pickle.dump(dic, f)
	Check_If_Time_Is_Over()


#Gui starts from here

time1 = Entry(root,textvariable=t).pack()  #taking entry as of time, name and item
name1 = Entry(root,textvariable=n).pack()
item1 = Entry(root, textvariable=i).pack()

ss = Button(root,text="done",command=SaveTheDaysToNotify).pack() #adding to the pickle database with format as dictionary as dic ={time:[name, item]}

root.mainloop()
Check_If_Time_Is_Over()
sorry for above post This is actually what i tried.
I want to get notification it the time in dic is equals to the now.
And how do i continusly check if the its true?
Reply


Forum Jump:

User Panel Messages

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