Самое простое решение - импортировать datetime
и узнать, какой это месяц. Затем проверьте текущий месяц, чтобы увидеть, хотите ли вы отобразить сообщение за этот месяц.
Tkinter поставляется с несколькими опциями для всплывающих сообщений. Я думаю, что для вашего конкретного вопроса вы хотите метод showinfo()
.
Вот простой пример:
import tkinter as tk
from tkinter import messagebox
from datetime import datetime
this_month = datetime.now().month
root = tk.Tk()
tk.Label(root, text="This is the main window").pack()
# this will display pop up message on the start of the program if the month is currently April.
if this_month == 4:
messagebox.showinfo("Message", "Some message you want the users to see")
root.mainloop()
Обновление:
Пытаясь помочь как ОП, так и другому человеку ответить на вопрос, я переформатировал их ответ на что-то более функциональное.
@ Джордж Сангер: Имейте в виду, что mainloop()
должен вызываться только в случае экземпляра Tk()
, из которого построены все приложения tkinter. Использование Toplevel
позволяет создавать новые окна после того, как вы уже создали главное окно с помощью Tk()
.
import tkinter as tk
percentage = 0.3
#tkinter applications are made with exactly 1 instance of Tk() and one mainloop()
root = tk.Tk()
def popupmsg(msg):
popup = tk.Toplevel(root)
popup.wm_title("!")
popup.tkraise(root) # This just tells the message to be on top of the root window.
tk.Label(popup, text=msg).pack(side="top", fill="x", pady=10)
tk.Button(popup, text="Okay", command = popup.destroy).pack()
# Notice that you do not use mainloop() here on the Toplevel() window
# This label is just to fill the root window with something.
tk.Label(root, text="\nthis is the main window for your program\n").pack()
# Use format() instead of + here. This is the correct way to format strings.
popupmsg('You have a discount of {}%'.format(percentage*100))
root.mainloop()