tkinter做一个倒计时弹窗

效果图:

tkinter做一个倒计时弹窗_第1张图片

通过clock_time参数定义倒计时的时间,当倒计时结束时,窗口会自动关闭。

root.protocol("WM_DELETE_WINDOW", no_closing) # 该指令设置点击窗口时执行的函数,本例为pass,没有做任何事,所以关闭按钮是无效状态。

import tkinter


def refresh_current_time():
    """刷新当前时间"""
    global clock_time
    detime.config(text=str(clock_time))
    clock_time -= 1
    detime.after(1000, refresh_current_time)
    if clock_time < 0:
        root.quit()


def no_closing():  # 设置关闭方式的函数
    """
    设置点击窗口关闭按钮时要做的事
    :return:
    """
    pass


root = tkinter.Tk()
root.wm_attributes('-topmost', 1)
# 设置窗体标题
root.title('Bot')
root.configure(bg='#FFFFFF')
# # 设置窗口大小和位置
width = 250
height = 140
# 找到电脑的长宽
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
# 设置窗口大小和位置
size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size_geo)

label = tkinter.Label(root, text='请等待数据刷新,请勿操作', bg='#FFFFFF', font=('微软雅黑', 10), foreground='#666666')
detime = tkinter.Label(root, text='60', bg='#5177c7', foreground='white', activebackground='#7492D2',
                       font=('微软雅黑', 12), relief='raised')

label.pack()
detime.pack()

label.place(x=30, y=25)
detime.place(x=width // 2 - 10, y=height // 2)
clock_time = 5  # 该变量定义倒计时时间
refresh_current_time()

root.protocol("WM_DELETE_WINDOW", no_closing)
root.mainloop()

你可能感兴趣的:(python全栈学习)