import pygame, easygui, sys, random
try:
import openpyxl
except ImportError:
print "Please install openpyxl. To do that use pip install openpyxl"
raise SystemExit
def main():
#All font and text
font = pygame.font.SysFont("times", 30)
choiceALabel = font.render("Choice A", 1, (255,255,255))
choiceBLabel = font.render("Choice B", 1, (255,255,255))
submitLabel = font.render("Pick one ->", 1, (255,255,255))
#End of font and text
attributesList = []
levelsDict = {}
attributesSurfaces = []
levelsSurfaces = []
excelInformation = {}
trial = 0
numTrials = 0
attributesPerPage = 0
try:
attributesFile = open("surveyinfo.txt", "r")
fileContents = attributesFile.read().split("\n")
#Checking to see if the file contains the magic number
if fileContents[0] != "040":
easygui.msgbox(msg="Survey Error. File not setup properly. Please use the conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
return
fileContentsNoMagicNumber = fileContents[1:]
#Iterate everything besides the magic number to build the attributes and levels
for i in range(len(fileContentsNoMagicNumber)):
if i != len(fileContentsNoMagicNumber) - 1:
attributesList.append(fileContentsNoMagicNumber[i].split(":")[0])
levelsDict[attributesList[i]] = fileContentsNoMagicNumber[i].split(":")[1].split(",")
else:
numTrials = int(fileContentsNoMagicNumber[i])
#If the file doesn't exist then exit the program
except IOError:
easygui.msgbox(msg="Survey Error. Missing file \"surveyinfo.txt\". Please use conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
return
if len(attributesList) > 5:
attributesPerPage = 5
else:
attributesPerPage = len(attributesList)
#print attributesPerPage
clock = pygame.time.Clock()
screen = pygame.display.set_mode((640,480), pygame.NOFRAME)
#Here we set a unique userID
userID = 1
try:
#Here we read from the file if we can and get the unique ID of the user
userIDFile = open("id.txt", "r")
userID = int(userIDFile.read())
userIDFile.close()
except IOError:
#If the file doesn't exist then we pass by the exception because it's not terrible
pass
#Here we replace the contents of the old file with a new userID
userIDFile = open("id.txt", "w")
userIDFile.write(str(userID+1))
userIDFile.close()
#End of unique userID
#Early questions here
age = int(easygui.enterbox("Please enter your age."))
gender = easygui.buttonbox(msg="Are you male or female?", choices=('Male', 'Female'), cancel_choice='Not Specified')
print gender
#End of early questions
running = True
createPage = True
aFirst = True
bFirst = False
while running:
if createPage:
isAFirst = random.randint(0,1)
if isAFirst:
aFirst = True
bFirst = False
else:
aFirst = False
bFirst = True
chosenAttributes = random.sample(attributesList, attributesPerPage)
chosenLevelsForA = []
chosenLevelsForB = []
for i in range(attributesPerPage):
chosenLevelsForA.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
chosenLevelsForB.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
attributesSurfaces = []
levelsSurfacesForA = []
levelsSurfacesForB = []
for i in range(attributesPerPage):
attributesSurfaces.append(font.render(chosenAttributes[i], 1, (255,255,255)))
levelsSurfacesForA.append(font.render(chosenLevelsForA[i], 1, (255,255,255)))
levelsSurfacesForB.append(font.render(chosenLevelsForB[i], 1, (255,255,255)))
createPage = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONUP:
mousePos = pygame.mouse.get_pos()
#Getting the click for the radio buttons
outcome = 0
clickedOnButton = False
if mousePos[0] >= 350 and mousePos[0] <= 370 and mousePos[1] >= 445 and mousePos[1] <= 465: #First button
clickedOnButton = True
if aFirst == True:
outcome = 1
else:
outcome = 2
elif mousePos[0] >= 535 and mousePos[0] <= 555 and mousePos[1] >= 445 and mousePos[1] <= 465: #Second button
clickedOnButton = True
if aFirst == True:
outcome = 3
else:
outcome = 4
excelInformation["Trial " + str(trial + 1)] = {}
for i in range(len(attributesList)):
excelInformation["Trial " + str(trial + 1)][attributesList[i]] = ""
excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = ""
excelInformation["Trial " + str(trial + 1)]["Neither?"] = ""
excelInformation["Trial " + str(trial + 1)]["Age"] = age
excelInformation["Trial " + str(trial + 1)]["Gender"] = gender
#print "Outcome: " + str(outcome)
if outcome == 1:#Picked B when B was first
for i in range(len(chosenAttributes)):
#print chosenAttributes[i] + ":" + chosenLevelsForA[i]#Wrong but it works
excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
if keepOption:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
else:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
elif outcome == 2:#Picked A when A was first
for i in range(len(chosenAttributes)):
#print chosenAttributes[i] + ":" + chosenLevelsForA[i]
excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
if keepOption:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
else:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
elif outcome == 3:#Picked A when B was first
for i in range(len(chosenAttributes)):
#print chosenAttributes[i] + ":" + chosenLevelsForB[i]#Wrong but it works
excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
if keepOption:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
else:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
elif outcome == 4:#Picked B when A was first
for i in range(len(chosenAttributes)):
#print chosenAttributes[i] + ":" + chosenLevelsForB[i]
excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
if keepOption:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
else:
excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
if clickedOnButton:
trial += 1
createPage = True
if trial == numTrials:
running = False
try:
wb = openpyxl.load_workbook('survey_results.xlsx')
ws = wb.worksheets[0]
except IOError:
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
ws.cell(row = 1, column = 1).value = "User #"
ws.cell(row = 1, column = 2).value = "Trial #"
for i in range(len(attributesList)):
ws.cell(row = 1, column = 3 + i).value = attributesList[i]
ws.cell(row = 1, column = 3 + len(attributesList)).value = "A/B"
ws.cell(row = 1, column = 4 + len(attributesList)).value = "Neither?"
ws.cell(row = 1, column = 5 + len(attributesList)).value = "Age"
ws.cell(row = 1, column = 6 + len(attributesList)).value = "Gender"
for i in range(numTrials):
#print userID
ws.cell(row = 2 + i + numTrials*(userID-1), column = 1).value = userID
ws.cell(row = 2 + i + numTrials*(userID-1), column = 2).value = i + 1
for j in range(len(attributesList)):
try:
ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + j).value = int(excelInformation["Trial " + str(i + 1)][attributesList[j]])
except ValueError:
ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + j).value = excelInformation["Trial " + str(i + 1)][attributesList[j]]
ws.cell(row = 2 + i + numTrials*(userID-1), column = 3 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Option Chosen"]
ws.cell(row = 2 + i + numTrials*(userID-1), column = 4 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Neither?"]
ws.cell(row = 2 + i + numTrials*(userID-1), column = 5 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Age"]
ws.cell(row = 2 + i + numTrials*(userID-1), column = 6 + len(attributesList)).value = excelInformation["Trial " + str(i + 1)]["Gender"]
wb.save("survey_results.xlsx")
#print excelInformation
#Drawing the UI
screen.fill((211,211,211))
pygame.draw.line(screen, (0,0,0), (453, 0), (453, 480))
pygame.draw.line(screen, (0,0,0), (266, 0), (266, 480))
pygame.draw.line(screen, (0,0,0), (0, 72), (640, 72))
pygame.draw.line(screen, (0,0,0), (0, 144), (640, 144))
pygame.draw.line(screen, (0,0,0), (0, 216), (640, 216))
pygame.draw.line(screen, (0,0,0), (0, 288), (640, 288))
pygame.draw.line(screen, (0,0,0), (0, 360), (640, 360))
pygame.draw.line(screen, (0,0,0), (0, 432), (640, 432))
pygame.draw.ellipse(screen, (255,255,255), (350,445,20,20))
pygame.draw.ellipse(screen, (0,0,0), (349,444,22,22), 1)
pygame.draw.ellipse(screen, (255,255,255), (535,445,20,20))
pygame.draw.ellipse(screen, (0,0,0), (534,444,22,22), 1)
screen.blit(choiceALabel, (303 + int(aFirst)*187, 20))
screen.blit(choiceBLabel, (303 + int(bFirst)*187, 20))
screen.blit(submitLabel, (screen.get_width()/5 - submitLabel.get_width()/2, screen.get_height() - 40))
#print chosenAttributes
#print chosenLevels
for i in range(0, attributesPerPage):
screen.blit(attributesSurfaces[i], (5, 90 + 72*i))
screen.blit(levelsSurfacesForA[i], (360 - levelsSurfacesForA[i].get_width()/2, 90 + 72*i))
screen.blit(levelsSurfacesForB[i], (545 - levelsSurfacesForB[i].get_width()/2, 90 + 72*i))
pygame.display.update()
msElapsed = clock.tick(60)
if __name__ == "__main__":
pygame.init()
main()
pygame.quit()
Comments
0 B
|👍
/👎