|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我尝试用tkinter做一个手速测试软件,每点击一次按钮就会把一个数字加1,同时还要计时,并且把时间显示在框架上。
我觉得这需要用到循环,但我发现一做循环tk就会停止运行,直到循环结束。
有没有大佬帮我改进,这是我的代码
- from tkinter import *
- import time as t
- class App:
- def __init__(self, root):
- self.frame = Frame(root)
- self.frame.pack()
- self.time = t.localtime()[6]
- self.count = 0
- self.var = StringVar()
- self.var.set(str(self.count))
- self.textlabel = Label(self.frame, textvariable = self.var)
- self.click = Button(self.frame, text = 'click me!', command = self.count_click)
- self.textlabel.pack()
- self.click.pack()
- def count_click(self):
- self.var.set(str(self.count + 1))
- self.count += 1
-
- root = Tk()
- root.geometry('150x150')
- app = App(root)
- root.mainloop()
复制代码
本帖最后由 kogawananari 于 2020-11-3 15:20 编辑
- from tkinter import *
- import time as t
- class App:
- def __init__(self, root):
- self.frame = Frame(root)
- self.frame.pack()
- self.start_time = None
- self.secs = 0
- self.count = 0
- self.var = StringVar()
- self.var.set(f'{self.secs}秒{self.count}次')
- self.textlabel = Label(self.frame, textvariable = self.var)
- self.click = Button(self.frame, text = 'click me!', command = self.count_click)
- self.textlabel.pack()
- self.click.pack()
- def count_click(self):
- if self.start_time is None:
- self.start_time = t.time()
- self.count = self.count + 1
- self.var.set(f'{self.secs}秒{self.count}次')
-
- def update_time(self):
- if self.start_time is not None:
- self.secs = int(t.time() - self.start_time)
- self.var.set(f'{self.secs}秒{self.count}次')
- self.frame.after(1000,self.update_time)
- root = Tk()
- root.geometry('150x150')
- app = App(root)
- app.update_time()
- root.mainloop()
复制代码
|
|