0

Thanks so much for Helping, It's fixed now thanks to you guys.

The problem was after I got my cps count the program delayed and froze. It stopped showing the cps.

I can't find out why this happens or how to fix it. The program is a cps counter, it works after 1 try then it breaks and freezes if I use it a second time.

I also have no idea where the code breaks, thank you so much.

Here is the code:

import threading
from tkinter import *
import time

clicks = 0
cps = 0
start = True

wn = Tk()
wn.title("Cps Counter")
wn.geometry("400x300")

def cps5():
    global start
    global cps
    global clicks
    global t1
    start = False
    time.sleep(5)
    cps = (clicks / 5)
    clicks = 0 
    CpsButton.config(text=clicks)
    CpsAmount.config(text=cps)
    start = True
    time.sleep(.1

t1 = threading.Thread(target=cps5)
t1.start()

def AddClicks():
    global clicks
    global start
    clicks += 1
    CpsButton.config(text=clicks)
    if start == True:
        cps5()

CpsButton = Button(wn, text=clicks, command=AddClicks, pady=15, padx=15)
CpsAmount = Label(wn, text=cps, pady=15, padx=15)

CpsAmount.pack()
CpsButton.pack()

wn.mainloop()
    
2
  • You need to be more specific about what you're doing, and what it means when you say it "breaks". minimal reproducible examples aren't just about the code, they're about how you're using it, what you expect to happen in that case, and what actually happens (if there is a traceback, provide it). Commented Feb 10, 2021 at 17:32
  • Also, this code is broken as written (you'll get a SyntaxError around the t1 = line due to an unclosed paren above it). And it's incredibly unsafe to do cross-thread tkinter calls like you're doing; use the event loop to schedule it with .after, don't launch a thread to call cps5. And don't use time.sleep; the entire GUI will freeze while it's sleeping (again, that's what .after is for); at a guess, you think it's broken because of the time.sleep, not realizing that the GUI is just going to be non-responsive for a titch over five seconds while cps5 is running. Commented Feb 10, 2021 at 17:34

1 Answer 1

1
import threading
from tkinter import *
import time

clicks = 0
cps = 0
start = True

wn = Tk()
wn.title("Cps Counter")
wn.geometry("400x300")
clicks = 0
def cps5():
    global start
    global cps
    global clicks
    global t1
    start = False

    CpsButton.config(text=clicks)
    start = True




def AddClicks():
    global clicks
    global start
    clicks += 1
    CpsButton.config(text=clicks)
    if start == True:
        cps5()

CpsButton = Button(wn, text=clicks, command=AddClicks, pady=15, padx=15)
CpsAmount = Label(wn, text=cps, pady=15, padx=15)

CpsAmount.pack()
CpsButton.pack()


def Timer():
    global CpsAmount, cps, clicks
    for i in range(0, 5):
        time.sleep(1)
    cps = (clicks / 5)
    CpsAmount.config(text=cps)
    clicks = 0
    Timer()

t1 = threading.Thread(target=cps5)
t1.start()
t2 = threading.Thread(target=Timer)
t2.start()
wn.mainloop()

I created a second thread for the counter, because time.sleep freezes the program and caused trouble.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.