from Tkinter import *
import random
root = Tk()
root.title="Space Game.py"
root.geometry=("1000x1000+500+300")
class Space(Frame):
def __init__(self, master):
**self.MetalResource = 1000000
self.CrystalResource = 1000000
self.GasResource = 1000000**
Frame.__init__(self, master, bg="#999999")
self.grid()
self.pack()
self.create_widgets()
def create_widgets(self):
self.Metal = Label(self, text="Metal", width=10, bg="#99CCFF")
self.Metal.grid(row=1, column=1)
self.MetalValue = Label(self, text=self.MetalResource, width=10, bg="#99CCFF")
self.MetalValue.grid(row=1, column=2)
self.Crystal = Label(self, text="Crystal", width=10, bg="#99CCFF")
self.Crystal.grid(row=2, column=1)
self.CrystalValue = Label(self, text=self.CrystalResource, width=10, bg="#99CCFF")
self.CrystalValue.grid(row=2, column=2)
self.Gas = Label(self, text="Gas", width=10, bg="#99CCFF")
self.Gas.grid(row=3, column=1)
self.GasValue = Label(self, text=self.GasResource, width=10, bg="#99CCFF")
self.GasValue.grid(row=3, column=2)
self.MetalMine = Button(self, text="Metal Mine", width=10, comman=self.MetalMineGUI, bg="#0000FF", fg="#FFFFFF")
self.MetalMine.grid(row=5, column=4)
self.MetalStorage = Button(self, text="Metal Storage", width=20, bg="#0000FF", fg = "#FFFFFF")
self.MetalStorage.grid(row=5, column=2)
def MetalMineGUI(self):
root2 = Tk()
root2.title="Beginner Level"
root2.geometry=("400x400+500+300")
class MetalMineGUI(Frame):
def __init__(self, master):
self.Level = 0
Frame.__init__(self, master, bg="#3399FF")
self.grid()
self.pack()
self.create_widgets()
def create_widgets(self):
if self.Level == 0:
a = "Build"
Metal = 500
Crystals = 250
Gas = 100
else:
a ="Upgrade"
Metal = 500*self.Level
Crystals = 250*self.Level
Gas = 100*self.Level
self.Upgrade = Button(self, text=a, width=10, command=self.Upgrade, bg="#99FFFF")
self.Upgrade.grid(row=3, column=5)
self.Details = Label(self, text="Details", width=30, bg="#3399FF")
self.Details.grid(row=1, column=1, columnspan=3)
self.level = Label(self, text="Level", width=5, bg="#3399FF")
self.level.grid(row=1, column=4)
self.lvl = Label(self, text=self.Level, width=5, bg="#3399FF")
self.lvl.grid(row=1, column=5)
self.Need = Label(self, text="Need:", width=10, bg="#3399FF")
self.Need.grid(row=2, column=1)
self.MetalNeeded = Label(self, text=Metal, width=10, bg="#3399FF")
self.MetalNeeded.grid(row=2, column=2)
self.CrystalsNeeded = Label(self, text=Crystals, width=10, bg="#3399FF")
self.CrystalsNeeded.grid(row=2, column=3)
self.GasNeeded = Label(self, text=Metal, width=10, bg="#3399FF")
self.GasNeeded.grid(row=2, column=4, columnspan=2)
def Upgrade(self):
self.Level+=1
**self.MetalResource-=Metal
self.CrystalResource-=Crystals
self.GasResource-=Gas**
app = MetalMineGUI(root2)
root.mainloop()
app = Space(root)
root.mainloop()
尝试获取self.MetalResource,self.CrystalResource和self.GasResource,当构建/升级按钮显示时要考虑到这一点。但是当它被点击时,它表示MetalMineGUI现在属于self.MetalResource。
答案 0 :(得分:1)
问题在于MetalResource
,CrystalResource
和GasResource
是Space
类的属性,而不是MetalMineGUI
类和MetalMineGUI
没有引用Space
类。
你定义了这样的类
class OuterClass():
def my_method(self):
class InnerClass():
pass
我不确定你的意图,但我认为你不需要这样做。而是尝试像
这样的东西class Space(Frame):
...
class MetalMineGui(Frame):
def __init__(self, master, space):
self.space = space
...
def Upgrade(self):
self.space.MetalResource -= Metal
...
同样如上所述,Metal未在Upgrade
函数的范围内定义。因此,您需要将成本计算移动到Upgrade
函数或类似的东西中。