鱼C论坛

 找回密码
 立即注册
查看: 2317|回复: 5

[已解决]tkinter循环问题

[复制链接]
发表于 2020-11-3 10:11:08 | 显示全部楼层 |阅读模式

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

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

x
我尝试用tkinter做一个手速测试软件,每点击一次按钮就会把一个数字加1,同时还要计时,并且把时间显示在框架上。
我觉得这需要用到循环,但我发现一做循环tk就会停止运行,直到循环结束。
有没有大佬帮我改进,这是我的代码
  1. from tkinter import *
  2. import time as t


  3. class App:
  4.         def __init__(self, root):
  5.                 self.frame = Frame(root)
  6.                 self.frame.pack()
  7.                 self.time = t.localtime()[6]
  8.                 self.count = 0
  9.                 self.var = StringVar()
  10.                 self.var.set(str(self.count))
  11.                 self.textlabel = Label(self.frame, textvariable = self.var)
  12.                 self.click = Button(self.frame, text = 'click me!', command = self.count_click)
  13.                 self.textlabel.pack()
  14.                 self.click.pack()

  15.         def count_click(self):
  16.                 self.var.set(str(self.count + 1))
  17.                 self.count += 1
  18.                        

  19. root = Tk()
  20. root.geometry('150x150')
  21. app = App(root)
  22. root.mainloop()
复制代码
最佳答案
2020-11-3 15:19:08
本帖最后由 kogawananari 于 2020-11-3 15:20 编辑
  1. from tkinter import *
  2. import time as t


  3. class App:
  4.     def __init__(self, root):
  5.         self.frame = Frame(root)
  6.         self.frame.pack()
  7.         self.start_time = None
  8.         self.secs = 0
  9.         self.count = 0
  10.         self.var = StringVar()
  11.         self.var.set(f'{self.secs}秒{self.count}次')
  12.         self.textlabel = Label(self.frame, textvariable = self.var)
  13.         self.click = Button(self.frame, text = 'click me!', command = self.count_click)
  14.         self.textlabel.pack()
  15.         self.click.pack()

  16.     def count_click(self):
  17.         if self.start_time is None:
  18.             self.start_time = t.time()
  19.         self.count = self.count + 1
  20.         self.var.set(f'{self.secs}秒{self.count}次')
  21.    
  22.     def update_time(self):
  23.         if self.start_time is not None:
  24.             self.secs = int(t.time() - self.start_time)
  25.             self.var.set(f'{self.secs}秒{self.count}次')
  26.         self.frame.after(1000,self.update_time)

  27. root = Tk()
  28. root.geometry('150x150')
  29. app = App(root)
  30. app.update_time()
  31. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-3 10:56:09 | 显示全部楼层
为什么觉得需要用到循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-3 11:50:21 | 显示全部楼层
kogawananari 发表于 2020-11-3 10:56
为什么觉得需要用到循环

需要查看时间到了没有啊。就算不用循环,用time.sleep也不行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-3 12:32:20 From FishC Mobile | 显示全部楼层
需要多线程吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-3 15:19:08 | 显示全部楼层    本楼为最佳答案   
本帖最后由 kogawananari 于 2020-11-3 15:20 编辑
  1. from tkinter import *
  2. import time as t


  3. class App:
  4.     def __init__(self, root):
  5.         self.frame = Frame(root)
  6.         self.frame.pack()
  7.         self.start_time = None
  8.         self.secs = 0
  9.         self.count = 0
  10.         self.var = StringVar()
  11.         self.var.set(f'{self.secs}秒{self.count}次')
  12.         self.textlabel = Label(self.frame, textvariable = self.var)
  13.         self.click = Button(self.frame, text = 'click me!', command = self.count_click)
  14.         self.textlabel.pack()
  15.         self.click.pack()

  16.     def count_click(self):
  17.         if self.start_time is None:
  18.             self.start_time = t.time()
  19.         self.count = self.count + 1
  20.         self.var.set(f'{self.secs}秒{self.count}次')
  21.    
  22.     def update_time(self):
  23.         if self.start_time is not None:
  24.             self.secs = int(t.time() - self.start_time)
  25.             self.var.set(f'{self.secs}秒{self.count}次')
  26.         self.frame.after(1000,self.update_time)

  27. root = Tk()
  28. root.geometry('150x150')
  29. app = App(root)
  30. app.update_time()
  31. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-3 17:01:55 | 显示全部楼层

原来有after方法,学到了,谢谢回答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 05:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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