[英]Python GUI shuts down when i use infinite loop
I tried to make a Clicker and I used an infinite loop, so I would raise my Variable every second. 我试图制作一个Clicker,并且使用了无限循环,所以我每秒都要提高我的Variable。 But every time I use the Button, my program crashes.
但是每次使用Button时,我的程序都会崩溃。 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()
Firstly, in your example bitcoins1
is undeclared. 首先,在您的示例中未声明
bitcoins1
。 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. 我认为这只是您在发布前忘记更改的变量名,因此我将其重命名为
clicks
以复制您的问题。
Second, you have your Elfhilft()
function using sleep()
, which is causing issues with your Tkinter app. 其次,您可以使用
sleep()
使用Elfhilft()
函数,这会导致Tkinter应用出现问题。 Tkinter uses its own loop system to handle real-time stuff, and sleep
will cause that loop to stall in most cases. Tkinter使用其自己的循环系统来处理实时信息,在大多数情况下,
sleep
会使该循环停止。 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. 我建议您使用
after
( 如何使用 tkinter 创建计时器? )的实现,以便复制假定您要实现的autoclicker-esque函数。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.