[英]How to create a loop that shuts down when given answer in python?
[英]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.