Search code examples
pythoneasygui

How do choiceboxes work in Python easygui?


I mean I've set up a choicebox.

    msg = "What would you like to do first?"
title = "Economy v1.0"
choices = ["Check your bank balance", 
           "Check the value of the stocks", 
           "Check what stocks you own", 
           "Buy stocks", 
           "Sell Stocks", 
           "Move to the next day.", 
           "Exit"]
choice = eg.choicebox(msg, title, choices)

But now I'd like to know how I get it to do something when a choice is selected. The tutorial on the website didn't give much help, and all the things I've experimented with haven't worked.

I tried to do this first:

if choice == choice[0]:
    #do stuff
if choice == choice[1]:
    #do more stuff
...

When I selected a choice, nothing happened.

So then I tried this:

if choice == "Buy Stocks":
    #do stuff
if choice == "Sell Stocks":
    #do stuff
...

But that didn't work either. I have tried a range of other things, but I'm not entirely sure what the choicebox returns, and what variable it stores it in. Could someone help me out here?


Solution

  • What you're doing should be working. Either of those if statements should be fine, just remember that strings are case sensitive.

    The easygui.choicesbox() is going to return a string to the variable you assign it to. In this case, it will ask for a selection, then stuff that value you selected into your variable choice. Remember that choicesbox always returns a string.

    if you try choice in choices it should return True.