im trying to make a Christmas countdown timer and i was almost done. i ran this code and it runs for a second and then closes everything with no errors. im not too advanced in python so if i made any stupid errors then thats why. could it be from threading? im really new to threading so it could be that.
heres my code right now.
import datetime
from tkinter import *
import time
from math import *
import threading
import datetime
import asyncio
import sys
root = Tk()
sys.setrecursionlimit(int(10e 6))
grid_placeholder = Label(root, text = " ", font=("Arial", 20))
grid_placeholder.grid(row = 1, column = 0)
seconds_check = ""
def xm():
dt = datetime.datetime
now = dt.now()
xmas_days = dt(year = 2021, month = 12, day = 25) - dt(year=now.year, month=now.month, day=now.day, hour=now.hour, minute=now.minute, second=now.second)
string_xmas1 = ""
string_xmas1 = string_xmas1 str(xmas_days)
hours = string_xmas1.rsplit(':', 2)[0]
hours = hours " hours, "
minutes = string_xmas1.rsplit(':', 2)[1]
minutes = minutes " minutes, "
seconds = string_xmas1.rsplit(':', 1)[1]
seconds = seconds " seconds"
xmas_days = hours minutes seconds
if xmas_days != seconds_check:
display_time = Label(root, text = xmas_days, font=("Arial", 13))
display_time.grid(row = 1, column = 0)
grid_placeholder.grid(row = 1, column = 0)
xmas_days = seconds_check
xm()
else:
display_time.destroy()
display_time = Label(root, text = xmas_days, font=("Arial", 13))
display_time.grid(row = 1, column = 0)
grid_placeholder.grid(row = 1, column = 0)
xm()
t1 = threading.Thread(target = xm)
t1.daemon = True
def thread():
t1.start()
button_time = Button(root, text= 'CLICK FOR COUNTDOWN', command = thread, height = 5, width = 20)
button_time.grid(row = 0, column = 0)
root.mainloop()
CodePudding user response:
You don't need to use threads here.
In tkinter you can use after()
method to call function after some period of time:
def xm():
# calculations and display
root.after(1000, xm)
button_time = Button(root, text= 'CLICK FOR COUNTDOWN', command = xm, height = 5, width = 20)
button_time.grid(row = 0, column = 0)
root.mainloop()