python - 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()
抱歉,如果我的答案已经得到回答,但我发现的所有其他问题都不能重现我所处的情况。
谢谢你的帮助 !
弗朗切斯科
解决方案
推荐阅读
- android - Xamarin.Android 启动画面大小
- javascript - 使用具有多个面板的一个表单时,如何在使用 PHP 提交表单后保持在同一个面板上
- r - group_rows() 命令中的换行时缩进 - R markdown 中的 kableExtra 包
- javascript - 如何获得推入数组的总和
- css - CSS Slider -- 在每张幻灯片上触发关键帧
- ubuntu - 从包中安装 docker
- javascript - 无法使用 HTML5 拖放 API 获取放置事件
- javascript - Highcharts 堆积柱形图(解析序列的 json 字符串)
- excel - 尝试使用生成的 CSV 文件中的 excel 公式
- javascript - Javascript 无法访问元素的“值”