我正在尝试为我的程序创建一个按钮界面,该界面作为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
答案 0 :(得分:0)
我想我最好把我的评论变成答案,所以我走了。
你对变量和属性之间的区别感到困惑,所以我将用一些例子来解释它们之间的区别。您的问题不是实际导入的问题,而是关于范围和面向对象编程(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
是属性,而不是局部变量。
第一个例子相当于:
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