I have a text based adventure which uses tkinter as its GUI, and am trying to create a function which will label buttons with different options, then return which button was pressed. I want to do this so it can be used as a general function for any decision in the game. I can create the buttons, but cannot get the function to return which button was clicked. This is roughly what I have so far:
from tkinter import *
import time
class Main(Frame):
def __init__(self, master):
Frame.__init__(self, master)
#configuring and placing frame
def change_1(self):
self.choice = 1
..............
def change_5(self):
self.choice = 5
def userChoice(self, o1, o2, o3, o4, o5, title, cmdlist)# cmdlist if there is less than 5 options
self.choice = 0
titleLable = Label(self, text = title)
#place & config
button_1 = Button(self, text = o1, command = self.change_1)
#place and configure buttons
..........
button_5 = Button(self, text = o5, command = self.change_5)
#place and configure more buttons
while self.choice not in cmdlist:
time.sleep(.5)
return self.choice
root = Tk()
main = Main(root)
cmdlist = [1, 2, 3, 4, 5]
cmd = main.createChoices("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Choose an Option", cmdlist)
#here would be something similar to: if cmd == 1:
do this etc.
root.mainloop()