鱼C论坛

 找回密码
 立即注册
查看: 59|回复: 4

[已解决]Tkinter 中的时间显示有问题

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. from tkinter import *
  2. import time
  3. root=Tk()
  4. e=Canvas(root,
  5.          width=436,
  6.          height=591,
  7.          background="khaki")
  8. e.pack()
  9. e.create_line(0,286,436,286,
  10.               fill="royalblue",
  11.               width=24,
  12.               arrow="both",
  13.               arrowshape=(244,344,244))
  14. def gyl():
  15.         d=time.strftime("%T")
  16.         root.after(1000,gyl)
  17.         e.create_text(200,275,
  18.               text=d,
  19.               font="方正流行体简体 44 bold",
  20.               fill="fuchsia")
  21. gyl()
  22. mainloop()
复制代码


最佳答案
3 天前
from tkinter import *
import time
root=Tk()
e=Canvas(root,
         width=436,
         height=591,
         background="khaki")
e.pack()
e.create_line(0,286,436,286,
              fill="royalblue",
              width=24,
              arrow="both",
              arrowshape=(244,344,244))
d = time.strftime("%T")
text_id = e.create_text(200,275,
      text=d,
      font="方正流行体简体 44 bold",
      fill="fuchsia")
def gyl():
    d = time.strftime("%T")
    e.itemconfig(text_id, text=d)
    root.after(1000, gyl)
gyl()
mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 3 天前 | 显示全部楼层
在Tkinter中显示动态时间时,主要问题是每次回调都创建新文本对象导致重叠。以下是修正方案:

问题分析:
1. 每次调用 gyl() 都在画布上新建文本,未清除旧文本
2. 未保存文本对象ID导致无法更新内容
3. 未正确管理定时器循环

修正代码:

  1. from tkinter import *
  2. import time

  3. root = Tk()
  4. e = Canvas(root,
  5.            width=436,
  6.            height=591,
  7.            background="khaki")
  8. e.pack()

  9. # 创建静态图形(只执行一次)
  10. e.create_line(0, 286, 436, 286,
  11.               fill="royalblue",
  12.               width=24,
  13.               arrow="both",
  14.               arrowshape=(244, 344, 244))

  15. # 预先创建文本对象并保存ID
  16. time_text = e.create_text(200, 275,
  17.                           text="",
  18.                           font="方正流行体简体 44 bold",
  19.                           fill="fuchsia")

  20. def update_time():
  21.     # 更新现有文本对象内容
  22.     current_time = time.strftime("%T")
  23.     e.itemconfig(time_text, text=current_time)  # 关键修改:更新而非新建
  24.     root.after(1000, update_time)  # 每秒触发

  25. update_time()  # 启动定时器
  26. mainloop()
复制代码


关键修改说明:
1. 使用 create_text() 预先创建文本对象并保存返回的ID
2. 在回调函数中使用 itemconfig() 更新现有文本
3. 定时器回调函数保持每秒更新

这样修改后:
√ 时间文本会每秒刷新而不会重叠
√ 减少资源占用(不再创建新对象)
√ 保持原有的图形样式不变

如果字体显示异常,请确认系统是否安装"方正流行体简体",或替换为通用字体如 "Arial 44 bold"

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 3 天前 | 显示全部楼层
用Label 动态显示更好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 3 天前 | 显示全部楼层    本楼为最佳答案   
from tkinter import *
import time
root=Tk()
e=Canvas(root,
         width=436,
         height=591,
         background="khaki")
e.pack()
e.create_line(0,286,436,286,
              fill="royalblue",
              width=24,
              arrow="both",
              arrowshape=(244,344,244))
d = time.strftime("%T")
text_id = e.create_text(200,275,
      text=d,
      font="方正流行体简体 44 bold",
      fill="fuchsia")
def gyl():
    d = time.strftime("%T")
    e.itemconfig(text_id, text=d)
    root.after(1000, gyl)
gyl()
mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 3 天前 | 显示全部楼层
看不懂,咋办
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 21:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表