Python 在导入的文件中找不到属性
我试图为我的程序制作一个按钮界面,该界面被导入为rlg。rlg有一个实时绘图系统,在该系统中,两个变量可以随着仿真的进行而测量和更新。我希望能够使这两个变量具有选择性,因此我在rlg的main()方法中制作了一个字典“graphline”,其中下拉菜单上的每个字符串选择都充当一个键。但是,我似乎无法访问它并获得错误消息:AttributeError:“function”对象没有属性“Graphline”。谁能看出我做错了什么Python 在导入的文件中找不到属性,python,import,tkinter,attributeerror,Python,Import,Tkinter,Attributeerror,我试图为我的程序制作一个按钮界面,该界面被导入为rlg。rlg有一个实时绘图系统,在该系统中,两个变量可以随着仿真的进行而测量和更新。我希望能够使这两个变量具有选择性,因此我在rlg的main()方法中制作了一个字典“graphline”,其中下拉菜单上的每个字符串选择都充当一个键。但是,我似乎无法访问它并获得错误消息:AttributeError:“function”对象没有属性“Graphline”。谁能看出我做错了什么 from Tkinter import * import runliv
from Tkinter import *
import runlivegraph3 as rlg
def run():
rlg.main()
def setLine1(name):
rlg.main.Line1data = rlg.main.graphLines[name] #graphlines is a dictionary in runlivegraph3 main method
def setLine2(name):
rlg.main.Line2data = rlg.main.graphLines[name]
root = Tk()
var1 = StringVar()
var1.set("select graph line 1 data") #initial variable in drop down menu, each string is a key in the graphLines dictionary
op1 = OptionMenu(root, var1, 'Political attacks in turn',
'Ethnic attacks in turn',
'Total attacks in turn',
'Ethnic attacks as a percentage of total attacks',
'Political attacks as a percentage of total attacks',
'Group 1 ethnic antagonism',
'Group 2 ethnic antagonism',
command = setLine1).pack()
var2 = StringVar()
var2.set("select graph line 2 data") #initial variable in drop down menu
op2 = OptionMenu(root, var2, 'Political attacks in turn',
'Ethnic attacks in turn',
'Total attacks in turn',
'Ethnic attacks as a percentage of total attacks',
'Political attacks as a percentage of total attacks',
'Group 1 ethnic antagonism',
'Group 2 ethnic antagonism',
command = setLine2).pack()
butn = Button(root, text = 'run', command = run)
butn.pack()
root.mainloop()
这是我在Tkinter button程序中导入的程序的main()函数
from matplotlib.pylab import *
import sys, random, time, csv
def main():
IDs = {}
boardDims = (20,20)
Line1data = None
Line2data = None
turnLimit = 40
pause = 0.0001
ethnicPred = []
politicalPred = []
totalAttacks = []
generation = []
data1 = []
data2 = []
data3 = []
ethAnt1 = []
ethAnt2 = []
polAnt1 = []
polAnt2 = []
EthnicAttacksInTurn = []
PoliticalAttacksInTurn = []
TotalAttacksInTurn = []
ProportionEth = []
ProportionPol = []
board = make_board(boardDims)
finallyAddAgents(IDs, board, boardDims)
splitAgents(IDs)
setRemainingPolitics(IDs)
setPoliticalAntagonism(IDs)
turn = 0
line1, = plot(turn, 0, 'b') #initialise lines
line2, = plot(turn, 0, 'r')
running = 1
while running:
ion() #sets up graph base and axes
axes()
xlim(0,turnLimit)
ylim(0,30)
if turn == turnLimit: running = 0
print_board3(IDs, board, boardDims)
print 'turn ', str(turn)
polAttackTurn = []
ethAttackTurn = []
AllAgentsPerformActions(IDs, board,turn,ethnicPred, politicalPred,
totalAttacks,polAttackTurn,ethAttackTurn)
totalAttackTurn = sum(ethAttackTurn) + sum(polAttackTurn)
if totalAttackTurn != 0:
propEth = (sum(ethAttackTurn)*100)/totalAttackTurn
propPol = (sum(polAttackTurn)*100)/totalAttackTurn
if totalAttackTurn == 0:
propEth = 0
propPol = 0
TotalAttacksInTurn.append(totalAttackTurn)
EthnicAttacksInTurn.append(sum(ethAttackTurn))
PoliticalAttacksInTurn.append(sum(polAttackTurn))
ProportionEth.append(propEth)
ProportionPol.append(propPol)
k = sum(politicalPred)
j = sum(ethnicPred)
#f = sum(totalAttacks)
#print k, j, f
data1.append(j)
data2.append(k)
#data3.append(f)
generation.append(turn)
for agent in IDs.values():
if agent.group == '1':
ethAnt1.append(agent.antagonism['2'])
break
for agent in IDs.values():
if agent.group == '2':
ethAnt2.append(agent.antagonism['1'])
break
for agent in IDs.values():
if agent.politics == 'A':
polAnt1.append(agent.polAntagonism['B'])
break
for agent in IDs.values():
if agent.politics == 'B':
polAnt2.append(agent.polAntagonism['A'])
break
#this is the dictionary i am trying to access from the Tkinter button program
graphLines = {'Political attacks in turn':sum(polAttackTurn),
'Ethnic attacks in turn':sum(ethAttackTurn),
'Total attacks in turn':totalAttackTurn,
'Ethnic attacks as a percentage of total attacks': propEth,
'Political attacks as a percentage of total attacks': propPol,
'Group 1 ethnic antagonism': ethAnt1[-1],
'Group 2 ethnic antagonism': ethAnt2[-1]}
line1.set_ydata(append(line1.get_ydata(), Line1data))
line1.set_xdata(append(line1.get_xdata(), turn))
line2.set_ydata(append(line2.get_ydata(), Line2data))
line2.set_xdata(append(line2.get_xdata(), turn))
draw()
turn += 1
我想我最好把我的评论变成一个答案,所以我来了 您正在混淆变量和属性之间的差异,因此我将用一些示例来解释这种差异。您的问题实际上不是导入的问题,而是关于范围和面向对象编程(OOP)的问题 (例如1)要在函数中设置局部变量,可以执行以下操作:
def spam():
eggs = 5
def spam():
pass
spam.eggs = 5
(例如2)要在函数对象上设置属性(通常不太符合逻辑),可以执行以下操作:
def spam():
eggs = 5
def spam():
pass
spam.eggs = 5
虽然这些可能看起来相似,但它们的效果却大不相同。在第一个示例中,eggs
是函数spam
中的局部变量。局部变量仅在其定义函数中创建、访问和修改
def spam():
eggs = 5
print spam.eggs
但是,将导致错误
def spam():
pass
spam.eggs = 5
print spam.eggs
不会。在第二个示例中,eggs
是函数(对象)spam
的一个属性。它可以在对象的方法内部或对象外部创建、访问和修改,但不能作为局部变量在函数本身内部创建、访问和修改(这也是因为函数在完全定义之前不知道它的存在)。因此,以下内容将引发错误:
def spam():
print eggs
spam.eggs = 5
spam()
因为eggs
是一个属性,而不是局部变量
如果您熟悉OOP,下面是一些扩展:
第一个例子相当于:
class Spam(object):
def __init__(self):
eggs = 5
class Spam(object):
def __init__(self):
self.eggs = 5
第二个例子相当于:
class Spam(object):
def __init__(self):
eggs = 5
class Spam(object):
def __init__(self):
self.eggs = 5
就OOP而言,区别只是第一个设置了一个局部变量,而第二个设置了一个实例变量。在第一个类上尝试执行Spam().eggs
是没有意义的,而在第二个类上则有意义
最后
要解决您的问题,请在函数外部定义所需的变量,或使用global
关键字显示它们是全局变量。用法示例:
def spam():
global eggs
eggs = 5
spam()
print eggs # Prints 5
究竟是什么让你认为你可以访问一个函数的变量作为该函数的属性?我甚至无法想象你认为这样的构造会做什么,或者你正试图做什么。所以我无法从按钮文件访问graphLines字典?我没有被正确地教过编程,所以我对“attribute”的含义只有一个模糊的概念。它没有正确地缩进,但是假设您的
graphLines=
行位于main()
函数中,graphLines
是该函数中的局部变量。字典仅在运行main()
时存在,并在函数结束时被丢弃。学习Python所遵循的任何Python教程都应该解释属性
的含义。因此,唯一可访问的变量必须是全局变量?那么为什么Line1data和Line2data不会引发相同的异常呢?