我正在尝试为我的程序,这是作为rlg导入的按钮界面。rlg有一个实时绘图系统,其中两个变量可以随着模拟的进行进行测量和更新。我希望能够选择性地选择这两个变量,所以我在rlg的main()方法中创建了一个字典'graphLines‘,其中下拉菜单上的每个字符串选择都充当一个键。然而,我似乎无法访问它并得到错误消息: AttributeError:'function‘对象没有'graphLines’属性。有人能看出我做错了什么吗?
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按钮程序中的程序的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 发布于 2012-08-07 01:22:14
我想我最好把我的评论变成一个答案,所以我开始了。
您对变量和属性之间的区别感到困惑,所以我将用一些示例来解释它们之间的区别。你的问题实际上不是导入的问题,而是更多关于作用域和面向对象编程(OOP)的问题。
(例如1)要在函数中设置局部变量,您可以执行以下操作:
def spam():
eggs = 5(例如2)要在函数对象上设置属性(通常不是那么符合逻辑),您可以执行以下操作:
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在OOP方面,区别很简单,第一个设置一个局部变量,而第二个设置一个实例变量。尝试在第一个类上做Spam().eggs是没有意义的,而在第二个类上可以。
最后,
要解决您的问题,可以在函数外部定义所需的变量,或者使用global关键字来显示它们是全局变量。示例用法:
def spam():
global eggs
eggs = 5
spam()
print eggs # Prints 5https://stackoverflow.com/questions/11831905
复制相似问题