0

I tried to make a Clicker and I used an infinite loop, so I would raise my Variable every second. But every time I use the Button, my program crashes. Do you have any advice how I prevent that, because I have no idea what is really happening.

import time
from tkinter import *

class Clicker :
    #updates the Label
    def AK_CLabel(self):
        self.ClickerLabel.configure(text="Du hast " + str(self.Clicks))

    #Generates Clicks
    def Klicken(self):
        self.Clicks += 1
        self.AK_CLabel()
    #raises price of Helping Elf and raises the clicks per second
    def HElf(self) :
        if(self.Clicks >= self.priceHElf) :
            self.Clicks -= self.priceHElf
            self.priceHElf = self.priceHElf * 1.2
            self.Elfs += 1
            self.Elfhilft()
            self.AK_CLabel()

    #Should make the Clicks go up by the amount of Elfs, but if I use the Button the Programm shuts down
    def Elfhilft(self):
        while (not time.sleep(5)):
            self.Clicks = self.Bitcoins1 + self.Elfs
            time.sleep(1)

    def __init__(self, master):
        self.master = master
        self.master.title = "Der Klicker"


        self.Elfs = 0
        self.priceHElf = 30
        self.Clicks = 30

        #Buttons and Label
        self.DerKnopf = Button(text = "Clicks", command = self.Klicken)
        self.ClickerLabel = Label(text = "You have " +str(self.Clicks))
        self.HelferElf = Button(text = "A helping Fairy", command = self.HElf)


        self.DerKnopf.pack()
        self.ClickerLabel.pack()
        self.HelferElf.pack()

root = Tk()
my_gui = Clicker(root)
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Crashes with what error? – Carcigenicate Nov 01 '17 at 14:06
  • And you probably shouldn't be using `sleep`. Tkinter will have its own timing function. Sleeping on an app thread is almost never a good idea. – Carcigenicate Nov 01 '17 at 14:07
  • I just stops working. – Lupuskiller Nov 01 '17 at 14:08
  • There will always be an error. What environment are you running in? Run this in a debugger and have it break on the exception. – Carcigenicate Nov 01 '17 at 14:09
  • `bitcoins1` is undeclared. That would be the first problem you should look at. – ividito Nov 01 '17 at 14:10
  • Sorry, i am not very experienced. But the Programm crashes and the only way to shut in down then is to click the X or wait until it shuts down itself. – Lupuskiller Nov 01 '17 at 14:17
  • I changed that, thank you ividito. – Lupuskiller Nov 01 '17 at 14:18
  • I am using Windows10 if this is helping – Lupuskiller Nov 01 '17 at 14:18
  • @Carcigenicate: no, there isn't always an error. In this case there is no error per se. The program seems to freeze because there is an infinite loop that puts the program to sleep. The GUI has no chance to respond to events. – Bryan Oakley Nov 01 '17 at 19:17
  • @BryanOakley "my program crashes". Arguably a misuse of "crash". I would say if your program truly crashes, it will always give some error. I don't think I've ever had a program shut right down abruptly and not give an error, at least not without forcing me to dig a bit. It wasn't clear until like four comments later that it was freeze. – Carcigenicate Nov 01 '17 at 19:20

1 Answers1

0

Firstly, in your example bitcoins1 is undeclared. I assume this is just a variable name you forgot to change before posting, so I renamed it to clicks in order to replicate your issue.

Second, you have your Elfhilft() function using sleep(), which is causing issues with your Tkinter app. Tkinter uses its own loop system to handle real-time stuff, and sleep will cause that loop to stall in most cases. I suggest you use an implementation of after (How to create a timer using tkinter?) in order to replicate the autoclicker-esque function I assume you're trying to implement. As an example:

def autoclick(self):
    self.clicks = self.clicks + self.Elfs

#In main app / __init__()
root.after(1000, self.autoclick) # updates auto-clicks every second
ividito
  • 321
  • 3
  • 14
  • Correction: sleep will cause mainloop to stall in all cases. It does exactly what the name implies: it puts the whole thread to sleep. Also, your example won't update every second, it updates exactly once because you call `after` only once. `after` doesn't auto-repeat. – Bryan Oakley Nov 01 '17 at 19:15