我一直在制作一个简单的Clicker GUI作为一个宠物项目,以提高我的编码技能,但是我遇到了一个绊脚石。我已经设法制作一个不会阻止我的GUI运行的循环,但我需要GUI每次循环运行时更新,因为GUI在一个单独的类中运行到我的循环。有没有可以帮助我的命令?
import tkinter
import math
import threading
import time
global Level
Level = 0
global Exp
Exp = 0
global Mana
Mana = 0
global Monsters
Monsters = 0
global Killed
Killed = 0
global Teachers
Teachers = 0
global Gold
Gold = 0
global Option
Option = 0
class Exp_per_second(object):
def __init__(self, interval=0.5):
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
while True:
global Exp
Exp = Exp + Teachers
time.sleep(self.interval)
class simpleapp_tk(tkinter.Tk):
def __init__(self, parent):
tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialise()
def initialise(self):
self.grid()
self.entryVariable = tkinter.StringVar()
self.entry = tkinter.Entry(self,textvariable=self.entryVariable)
self.labelVariable1 = tkinter.StringVar()
label1 = tkinter.Label(self, textvariable=self.labelVariable1, anchor='w', fg='black')
label1.grid(column=0, row=1, columnspan=2, stick='EW')
self.labelVariable1.set(u"Experience:")
self.labelVariable2 = tkinter.StringVar()
label2 = tkinter.Label(self, textvariable=self.labelVariable2, anchor='w', fg='black')
label2.grid(column=2, row=1, stick='EW')
self.labelVariable2.set(Exp)
button1 = tkinter.Button(self, text=u" Learn ", command=self.OnButtonClick1)
button1.grid(column=0, row=4, columnspan=2)
def New_Option1(self):
self.labelVariable3 = tkinter.StringVar()
label3 = tkinter.Label(self, textvariable=self.labelVariable3, anchor='w', fg='black')
label3.grid(column=0, row=2,columnspan=2, stick='EW')
self.labelVariable3.set(u" Mana:")
self.labelVariable4 = tkinter.StringVar()
label4 = tkinter.Label(self, textvariable=self.labelVariable4, anchor='w', fg='black')
label4.grid(column=2, row=2, stick='EW')
self.labelVariable4.set(Mana)
self.labelVariable5 = tkinter.StringVar()
label5 = tkinter.Label(self, textvariable=self.labelVariable5, anchor='w', fg='black')
label5.grid(column=0, row=0, stick='EW')
self.labelVariable5.set('Level:')
self.labelVariable6 = tkinter.StringVar()
label6 = tkinter.Label(self, textvariable=self.labelVariable6, anchor='w', fg='black')
label6.grid(column=1, row=0, stick='EW')
self.labelVariable6.set(Level)
button2 = tkinter.Button(self, text=u"Concentrate", command=self.OnButtonClick2)
button2.grid(column=2, row=4)
def New_Option2(self):
self.labelVariable6.set(Level)
self.labelVariable7 = tkinter.StringVar()
label7 = tkinter.Label(self, textvariable=self.labelVariable7, anchor='w', fg='black')
label7.grid(column=0, row=3, columnspan=2, stick='EW')
self.labelVariable7.set(' Monsters:')
self.labelVariable8 = tkinter.StringVar()
label8 = tkinter.Label(self, textvariable=self.labelVariable8, anchor='w', fg='black')
label8.grid(column=2, row=3, stick='EW')
self.labelVariable8.set(Monsters)
button3 = tkinter.Button(self, text=u" Summon ", command=self.OnButtonClick3)
button3.grid(column=0, row=5, columnspan=2)
button4 = tkinter.Button(self, text=u" Fireball ", command=self.OnButtonClick4)
button4.grid(column=2, row=5)
def New_Option3(self):
self.labelVariable6.set(Level)
self.labelVariable9 = tkinter.StringVar()
label9 = tkinter.Label(self, textvariable=self.labelVariable9, anchor='w', fg='black')
label9.grid(column=3, row=0, stick='EW')
self.labelVariable9.set('Gold:')
self.labelVariable10 = tkinter.StringVar()
label10 = tkinter.Label(self, textvariable=self.labelVariable10, anchor='w', fg='black')
label10.grid(column=4, row=0, stick='EW')
self.labelVariable10.set(Gold)
self.labelVariable11 = tkinter.StringVar()
label11 = tkinter.Label(self, textvariable=self.labelVariable11, anchor='w', fg='black')
label11.grid(column=3, row=1, columnspan=2, stick='EW')
self.labelVariable11.set('Teachers:')
self.labelVariable12 = tkinter.StringVar()
label12 = tkinter.Label(self, textvariable=self.labelVariable12, anchor='w', fg='black')
label12.grid(column=5, row=1, stick='EW')
self.labelVariable12.set(Teachers)
button5 = tkinter.Button(self, text=u" Hire ", command=self.OnButtonClick5)
button5.grid(column=3, row=4, columnspan=2)
button6 = tkinter.Button(self, text=u" Teacher ", command=self.OnButtonClick6)
button6.grid(column=3, row=5, columnspan=2)
def OnButtonClick1(self):
global Exp
Exp = Exp + 2**Monsters
self.labelVariable2.set(Exp)
if Exp == 10 and Level == 0:
global Level
Level = 1
self.New_Option1()
def OnButtonClick2(self):
if Exp >= 10:
global Exp
Exp = Exp - 10
global Mana
Mana = Mana + 1
self.labelVariable2.set(Exp)
self.labelVariable4.set(Mana)
if Mana == 5 and Level == 1:
global Level
Level = 2
self.New_Option2()
def OnButtonClick3(self):
if Mana >= 5:
global Exp
Exp = Exp + 5
global Mana
Mana = Mana - 5
global Monsters
Monsters = Monsters + 1
self.labelVariable2.set(Exp)
self.labelVariable4.set(Mana)
self.labelVariable8.set(Monsters)
def OnButtonClick4(self):
if Monsters >= 1 and Mana >= 10:
global Exp
Exp = Exp + 20
global Mana
Mana = Mana - 10
global Monsters
Monsters = Monsters - 1
global Killed
Killed = Killed + 1
global Gold
Gold = Gold + 10
if Level >= 3:
self.labelVariable10.set(Gold)
self.labelVariable2.set(Exp)
self.labelVariable4.set(Mana)
self.labelVariable8.set(Monsters)
if Killed == 5 and Level == 2:
Level = 3
self.New_Option3()
def OnButtonClick5(self):
if Gold == 50 and Option == 1:
global Gold
Gold = Gold - 50
global Teachers
Teachers = Teachers + 1
self.labelVariable10.set(Gold)
self.labelVariable12.set(Teachers)
def OnButtonClick6(self):
global Option
Option = 1
button6 = tkinter.Button(self, text=u" Teacher ", command=self.OnButtonClick6, bg='gray')
button6.grid(column=3, row=5, columnspan=2)
if __name__ == "__main__":
Count1 = Exp_per_second()
app = simpleapp_tk(None)
app.title('Clicker')
app.mainloop()
我知道我犯了一些错误并且它非常混乱但我没有使用python很长时间这是我尝试学习的。这也是我第一次使用这个网站提出问题,但我确实尝试了一些研究,我似乎无法在任何地方找到解决方案。任何改进代码的建议都会很好。
答案 0 :(得分:0)
您可以将app
对象作为参数发送到Exp_per_second()
(仅在创建应用程序后创建它),然后将其设置为Exp_per_second()
类中的实例变量,然后在while循环内的run()
方法中访问它。然后您可以随意对其进行更改。
示例/演示(此示例仅用于说明其如何工作) -
Exp_per_second()类更改为 -
class Exp_per_second(object):
def __init__(self, app, interval=0.5):
self.interval = interval
self.app = app
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
while True:
global Exp
Exp = Exp + Teachers
self.app.labelVariable1.set(u"Experience:"+str(Exp))
time.sleep(self.interval)
该计划的主要部分如下 -
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('Clicker')
Count1 = Exp_per_second(app)
app.mainloop()
在你的情况下你可以做这样的事情。