我正在尝试为作为 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