首页 > 解决方案 > Tkinter 窗口有时会消失

问题描述

再会 !

我在使用 tkinter 时遇到了我自己无法解决的问题。我想在位于我的任务栏上方的窗口上显示倒计时,如下图所示。

我想做的是让我的程序在启动时运行并在我关闭计算机时关闭。我真的需要这个窗口在我的任务栏上,我也需要它永久。我的问题是它有时会消失。

在进行各种测试时,我注意到了不同的情况:

我已阅读问题的答案 为什么我的 Tkinter 窗口有时根本不显示?并测试了线路的几个位置

root.overrideredirect(1)

但似乎什么都没有改变。这是我的代码:

import tkinter as tk
from datetime import datetime
from dateutil import relativedelta
def diff(): #Computes the difference , in days/hours/minutes, between two dates 
    """
    Returns a string
    """
    

    date_2 = datetime(2020, 5, 4, 8, 00) 
    

    date_1 = datetime.now()
    
    #This will find the difference between the two dates
    difference = relativedelta.relativedelta(date_2, date_1)
    

    months = difference.months
    days = difference.days+months*30
    hours = difference.hours
    minutes = difference.minutes
    
    text=(str(days)+" jours "+str(hours)+" heures "+str(minutes)+" minutes ")
    return text #string

def update(): #Updating the window
    texte=str(diff())
    
    l.config(text="Temps restant avant CCP:\n "+texte) #That's the string that needs to update every minute
    root.after(1000, update)



root = tk.Tk()
root.overrideredirect(1)
root.wm_attributes("-topmost", 1) #This should make my window appear above everything
root.geometry("{0}x{1}+1300+1040".format(300, 40))
root.resizable(width=False, height=False) #Removing it doesn't change my problem
root['bg']="#d5dadc" #So that the background of the window matches my taskbar's color

l = tk.Label(text='Init...',bg="#d5dadc")
l.pack()

root.after(1000, update) #starts updating after 1 minute

root.mainloop()

抱歉,如果我的答案已经得到回答,但我发现的所有其他问题都不能重现我所处的情况。

谢谢你的帮助 !

弗朗切斯科

标签: pythontkinter

解决方案


推荐阅读