I wanted to make a text-based adventure game. It seemed easy enough, I just make a gigantic tree of if statements and get input from the user at every step. However, then I decided to try and make a GUI using Tkinter. The problem is I am unaware of how to get input from one of the buttons and progress the game.
I have tried implementing a choice variable as ch. The buttons Trigger the method that changes ch. I was hoping I could detect if an input was made and get the ch variable. However, the if statement won't trigger despite being in the main loop.
here is the full code:
import tkinter as tk
root = tk.Tk()
#self.root.iconbitmap('')
root.geometry("800x500")
root.title("Diego's Adventure Extravaganza!")
class GUI:
def __init__(self,master):
#the charecter will be able to make choices
self.ch =5
self.index=0
#this will display the chapter the charecter is on
self.title = tk.Label(master, text="Text for titile", font=('Arial',18))
self.title.pack()
#relivent story details
self.storyframe = tk.Frame(master)
self.storyframe.columnconfigure(0, weight=1)
self.storyframe.columnconfigure(1, weight=1)
self.photo = tk.PhotoImage(file='images/pngtest.png')
self.image = tk.Label(self.storyframe, image=self.photo, width= 300, height = 150)
self.image.grid(row=0,column=0,sticky=tk.W+tk.E)
self.story= tk.Label(self.storyframe, text="this is where the story goes")
self.story.grid(row=0,column=1,sticky=tk.W+tk.E)
self.storyframe.pack(fill='x')
self.buttonframe=tk.Frame(master)
self.buttonframe.columnconfigure(0,weight=1)
self.buttonframe.columnconfigure(1,weight=1)
#the choices the chatecter can make at the moment
self.btmZ= tk.Button(self.buttonframe,text="The zero's button", command= self.clickZ)
self.btmZ.grid(row=0, column=0, sticky=tk.W+tk.E)
self.btmO= tk.Button(self.buttonframe,text="the first button", command= self.clickO)
self.btmO.grid(row=0, column=1, sticky=tk.W+tk.E)
self.btmT= tk.Button(self.buttonframe,text="the third button", command= self.clickT)
self.btmT.grid(row=1, column=0, sticky=tk.W+tk.E)
self.btmTh= tk.Button(self.buttonframe,text="the fourth button",command= self.clickTh)
self.btmTh.grid(row=1, column=1, sticky=tk.W+tk.E)
self.buttonframe.pack(fill='x')
def clickZ(self):
self.ch=0
def clickO(self):
self.ch=1
def clickT(self):
self.ch=2
def clickTh(self):
self.ch=3
def cTitle(self,ctext):
self.title.config(text=ctext)
def cImage(self,cpath):
self.photo.config(file=cpath)
def cStory(self,ctext):
self.story.config(text=ctext)
def cBtmText(self,ctext,num):
if num==0:
self.btmZ.config(text=ctext)
elif num == 1:
self.btmO.config(text=ctext)
elif num == 2:
self.btmT.config(text=ctext)
else:
self.btmTh.config(text=ctext)
win = GUI(root)
#The main program
#trying to test the buttons, I want a branching story and for the buttons to do many things
if win.ch ==0:
win.cTitle("Chapter 1")
elif win.ch == 1:
win.cStory("The story begins")
elif win.ch == 2:
win.cbtmText("This is the best button", 2)
else:
win.ch =5
root.mainloop()
Here is a bad solution: you have to exit the program to step forward.