【发布时间】:2018-07-10 22:00:49
【问题描述】:
我想制作一个脚本或一个小程序来提供我的 cps(每秒点击次数),但我发现一个小问题是制作一个 10 秒的计时器,同时单击左键单击按钮。我尝试了模块线程,但它不适用于 tkinter 我已经尝试了所有方法(让函数内的计时器执行几个函数以增加变量 ex ...中的计时器)但我从来没有设法同时做到这一点。我的程序应该看起来像什么可以使这个网站:www.mcrpg.com/kohi-click-test
Ps:要测试我的问题,请单击开始而不是测试开始。
import time
import os
from tkinter import *
from tkinter.constants import *
from threading import Thread
class Interface(Frame):
def run(self):
thread1 = Thread(target = self.Démarrer )
thread2 = Thread(target = self.timer)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
fenetre.update()
def timer(self):
length = 10
for i in range(1,(length+1)):
print(i)
self.Label2["text"] = "Le temps est {}".format(i)
fenetre.update()
time.sleep(1)
def MaApp(self):
self.nb_clic += 1
self.cps = (self.nb_clic / 10)
self.Label["text"]="Le Nombre de clic est de {}".format(self.nb_clic)
self.Label1["text"] = "Votre cps est de {}".format(self.cps)
fenetre.update()
def Démarrer(self):
self.bouton_cliquer["text"]= "Clic Gauche"
self.bouton_cliquer["command"] = self.MaApp
fenetre.update()
def __init__(self, fenetre, **kwargs):
Frame.__init__(self, fenetre, **kwargs)
self.pack(fill=BOTH)
fenetre.geometry("400x200+300+300")
fenetre.title("ClickTest")
#Variable
self.nb_clic = 0
self.cps = (self.nb_clic / 10)
self.temps = 0
# Création de nos widgets
self.Label = Label(self , text="Le Nombre de clic est de {}".format(self.nb_clic))
self.Label.pack()
self.Label1 = Label(self, text="Votre cps est de {}".format(self.cps))
self.Label1.pack()
self.Label2 = Label(self , text=("Le temps est {}").format(self.temps))
self.Label2.pack()
self.bouton_quitter = Button(self, text="Quitter",
command=self.quit
)
self.bouton_quitter.pack(side="left")
self.bouton_cliquer = Button(self, text="Démarrer" ,
command=self.run
)
self.bouton_cliquer.pack(side="right")
fenetre.update()
# Bouton de Test
self.bouton_cliquer2 = Button(self, text="Test Démarrer",
command = self.Démarrer
)
self.bouton_cliquer2.pack()
self.bouton_cliquer3 = Button(self, text="Test MaApp",
command = self.MaApp
)
self.bouton_cliquer3.pack()
self.bouton_cliquer4 = Button(self, text="Test Timer",
command = self.timer
)
self.bouton_cliquer4.pack()
if __name__ == '__main__':
fenetre = Tk()
interface = Interface(fenetre)
interface.mainloop()
interface.destroy()
【问题讨论】:
-
您不能使用后台线程中的任何 tkinter 对象。有解决方案,但它们都很痛苦。如果您愿意重新考虑您的程序以使其不需要线程,那将需要进行更深入的更改,但它们会更易于理解和管理。你想要哪个答案?
标签: python multithreading tkinter