属性错误:' str'对象没有属性

时间:2016-01-03 22:20:16

标签: python string attributeerror

我正在尝试将SiteID的数据条目存储在网站类变量siteID中,但我收到此属性错误。有人能告诉我哪里出错了吗? 错误说明:AttributeError: 'str' object has no attribute 'siteID'

   from tkinter import *

#Define class for site

class Site(object):
    def __init__(self, siteID, NoFuses, NoCbs, NoPumps):
        self.siteID = siteID
        self.NoFuses = NoFuses
        self.NoCbs = NoCbs
        self.NoPumps = NoPumps

    def getSiteID(self):
        return self.siteID

    def getNoFuses(self):
        return self.NoFuses

    def getNoCbs(self):
        return self.NoCbs

    def getNoPumps(self):
        return self.NoPumps

#define class for each network element:
#Transformer, CB's, Fuses, Cables, Pumps, VFD's

class Transformer(object):
    def __init__(self, TXname, Vprim, Vsec, Imped, phaseShift, XtoR):
        self.TXname = TXname
        self.Vprim = Vprim
        self.Vsec = Vsec
        self.Imped = Imped
        self.phaseShift = phaseShift
        self.XtoR = XtoR

    def getTXname(self):
        return self.TXname

    def getVprim(self):
        return self.Vprim

    def getVsec(self):
        return self.Vsec

    def getImped(self):
        return self.Imped

    def getPhaseShift(self):
        return self.phaseShift

    def getXtoR(self):
        return self.XtoR

class CircuitBreaker(object):
    def __init__(self, equipID, Ir, Im, Isc, tripTime):
        self.equipID = equipID #as read on drawings
        self.Ir = Ir
        self.Im = Im
        self.Isc = Isc #Device current capacity (kA)
        self.tripTime = tripTime

    def getEquipID(self):
        return self.equipID

    def getIr(self):
        return self.Ir

    def getIm(self):
        return self.Im

    def getIsc(self):
        return self.Isc 
    def getTripTime(self):
        return self.tripTime

class Fuse(object):
    def __init__(self, fuseType, breakCurr, trip, time):
        self.fuseType = fuseType #HV or LV
        self.breakCurr = breakCurr #device current capacity (kA)
        self.trip = trip #device trip current
        self.time = time #trip time

    def getFuseType(self):
        return self.fuseType

    def getBreakCurr(self):
        return self.breakCurr

    def getTrip(self):
        return self.trip

    def getTime(self):
        return self.time

class VFD(object):
    def __init__(self, tagNo, PF, kW, Volts, servFact):
        self.tagNo = tagNo
        self.PF = PF
        self.kW = kW
        self.Volts = Volts
        self.servFact = servFact

    def getTagNo(self):
        return self.tagNo

    def getPF(self):
        return self.PF

    def getkW(self):
        return self.kW

    def getVolts(self):
        return self.Volts

    def getServFact(self):
        return self.servFact

class Cable(object):
    def __init__(self, cableName, cond, insu, lenth, NoCond, config):
        self.cableName = cableName
        self.cond = cond #conductor type
        self.insu = insu #insulation type
        self.length = length
        self.NoCond = NoCond #number of conductors per phase
        self.config = config #cable configuration (3C+E etc.)

    def getCableName(self):
        return self.cableName

    def getCond(self):
        return self.cond

    def getInsu(self):
        return self.insu

    def getLength(self):
        return self.length

    def getNoCond(self):
        return self.NoCond

    def getConfig(self):
        return self.config

class Pump(object):
    def __init__(self, pumpID, powerFactor, power, poles, voltage, loadFactor):
        self.pumpID
        self.powerFactor = powerFactor
        self.power = power
        self.poles = poles
        self.voltage = voltage
        self.loadFactor = loadFactor

    def getPumpID(self):
        return self.PumpID

    def getPowerFactor(self):
        return self.powerFactor

    def getPower(self):
        return self.power

    def getPoles(self):
        return self.poles

    def getVoltage(self):
        return self.voltage

    def getLoadFactor(self):
        return self.loadFactor

#create gui
#write variable windows for each class. call these functions in mainwindow()

def storeSite(entry):
    #stores site ID in site class 
    siteID_str = Site.getSiteID(entry.get())

#def storeFuse():
    #stores number of fuses in site class
 #   NoFuses_str = Site.getNoFuses(e1.get())

#def storeCB():
    #stores number of circuit breakers in site class
 #   NoCbs_str = Site.getNoCbs(e2.get())

#def storePump():
    #stores number of pumps in site class
 #   NoPumps_str = Site.getNoPumps(e3.get())

def VariableWindow():
    #creates windows based on MainWindow data entries
    window = Toplevel() 
    window.title('Site Elements')
    window.focus_set()

def MainWindow():     
    root = Tk()
    root.title("Site") #initial window
    root.geometry("500x150")
    app = Frame(root)
    app.grid()

    l = Label(app, text = "enter site ID:")
    l.grid(row=0, column=0)
    e = Entry(app)
    e.grid(row=0,column=1) 
    b = Button(app, text = "ok", command = storeSite(e)) 
    b.grid(row=0,column=2)

    l1 = Label(app, text = "Enter Number of Fuses (include Transformer HV fuse):")
    l1.grid(row=1, column=0)
    e1 = Entry(app)
    e1.grid(row=1, column=1) 
    b1 = Button(app, text = "ok") 
    b1.grid(row=1,column=2)

    l2 = Label(app, text = "Enter Number of Circuit Breakers:")
    l2.grid(row=2, column=0)
    e2 = Entry(app)
    e2.grid(row=2, column=1) 
    b2 = Button(app, text = "ok") 
    b2.grid(row=2, column = 2)

    l3 = Label(app, text = "Enter Number of Pumps:")
    l3.grid(row=3, column = 0)
    e3 = Entry(app)
    e3.grid(row=3, column=1) 
    b3 = Button(app, text = "ok") 
    b3.grid(row=3, column=2)

    b4 = Button(app, text = "Finish", command = VariableWindow()) #command to prompt next window
    b4.grid(row = 5)
    root.mainloop()


#When all data is stored in classes, populate (search replace) 'export' and .dwr files

2 个答案:

答案 0 :(得分:2)

考虑失败代码的最小版本:

class A(object):
    def x(self):
        return self.attr

A.x('abc')

您没有调用A类对象的方法。您正在调用A本身的类函数并传递一个字符串,其中self是预期的。

同样的事情发生在Site.getSiteID(entry.get())的应用中。

您可能需要的是一些可以访问的全局(或窗口本地)站点对象。

答案 1 :(得分:1)

你的问题在这里:

siteID = Site.getSiteID(entry.get())

您正在使用它类似于类方法,而不是实例方法。因此entry.get()正在传递而不是self。代码尝试返回self.SiteID此处,selfentry.get(),这是一个字符串且没有SiteID

你应该做这样的事情:

site = Site(*params)
site.getSiteID()