繁体   English   中英

当我使用无限循环时,Python GUI关闭

[英]Python GUI shuts down when i use infinite loop

我试图制作一个Clicker,并且使用了无限循环,所以我每秒都要提高我的Variable。 但是每次使用Button时,我的程序都会崩溃。 您对我如何防止这种情况有任何建议,因为我不知道真正发生了什么。

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()

首先,在您的示例中未声明bitcoins1 我认为这只是您在发布前忘记更改的变量名,因此我将其重命名为clicks以复制您的问题。

其次,您可以使用sleep()使用Elfhilft()函数,这会导致Tkinter应用出现问题。 Tkinter使用其自己的循环系统来处理实时信息,在大多数情况下, sleep会使该循环停止。 我建议您使用after如何使用 tkinter 创建计时器? )的实现,以便复制假定您要实现的autoclicker-esque函数。 举个例子:

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

#In main app / __init__()
root.after(1000, self.autoclick) # updates auto-clicks every second

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM