Python 当我使用无限循环时 GUI 关闭
Python GUI shuts down when i use infinite loop
我尝试制作一个 Clicker 并且我使用了一个无限循环,所以我每秒都会增加我的 Variable。但是每次我使用按钮时,我的程序都会崩溃。
你对我如何防止这种情况有什么建议吗,因为我不知道到底发生了什么。
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
以复制您的问题。
其次,您的 Elfhilft()
函数使用 sleep()
,这会导致您的 Tkinter 应用出现问题。 Tkinter 使用它自己的循环系统来处理实时的东西,并且 sleep
在大多数情况下会导致循环停止。我建议您使用 after
(How to create a timer using tkinter?) 的实现来复制我假设您正在尝试实现的类似自动点击器的功能。例如:
def autoclick(self):
self.clicks = self.clicks + self.Elfs
#In main app / __init__()
root.after(1000, self.autoclick) # updates auto-clicks every second
我尝试制作一个 Clicker 并且我使用了一个无限循环,所以我每秒都会增加我的 Variable。但是每次我使用按钮时,我的程序都会崩溃。 你对我如何防止这种情况有什么建议吗,因为我不知道到底发生了什么。
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
以复制您的问题。
其次,您的 Elfhilft()
函数使用 sleep()
,这会导致您的 Tkinter 应用出现问题。 Tkinter 使用它自己的循环系统来处理实时的东西,并且 sleep
在大多数情况下会导致循环停止。我建议您使用 after
(How to create a timer using tkinter?) 的实现来复制我假设您正在尝试实现的类似自动点击器的功能。例如:
def autoclick(self):
self.clicks = self.clicks + self.Elfs
#In main app / __init__()
root.after(1000, self.autoclick) # updates auto-clicks every second