I am making a story-based game on Tkinter and I what I want is that once the play clicks the next button, or makes a decision I want that block of text to disappear so the player can focus on the next set of text and decisions.
I tried to make a labelname.destroy and a labelname.pack_forget in the functions that the user chooses for example, the user chooses the button 'join' then the program would go into def join():, and inside that function would be labelname.destroy, so that block of text would delete, but it's not working for some reason. So now I've attempted to create a clear_label function that I can call instead, but that,s not working either. Please help!
code:
from tkinter import *
from tkinter import ttk
from tkinter.simpledialog import askstring
from tkinter.messagebox import showinfo
from PIL import Image, ImageTk
#Tkinter window
window= Tk()
window.geometry("600x400") #size of window
window.title("Escape the Addiction: A Quest for Balance")
#defining the score value inorder to give different outputs when the next button is clicked
score = 1
def ask_name():
global name
window.iconify() #minimize main window
#asks user for name input
name = askstring('Name', 'What is your name?')
showinfo('Hello!', 'Hi, {}'.format(name))
game()
#when the start button is clicked a new screen window is opened which is the actual game
def game():
global dialogue_1, dialogue_2, Leave, Join, dialogue_3, dialogue_4, game_Screen, next_button, next_button1
#window theme
style = ttk.Style()
game_screen = Toplevel(window)
game_screen.geometry("600x400")
game_screen.title("The Quest")
print("You are now in the new window!")
#when the next button gets clicked
def next():
global score
if score == 0:
story_line.config(text='Welcome to Escape the Addiction: A Quest for Balance, {}'.format(name))
print('The text is changing')
score +=1
elif score == 1:
story_line.config(text='This is an interactive story game!')
print('The text is changing again')
score +=1
elif score == 2:
story_line.config(text='where you will be given a scenario and will have the power to drive the story.')
print('The text is changing again')
score +=1
elif score == 3:
story_line.config(text='But be careful, your decisions will result in your ending.')
print('The text is changing again')
score +=1
elif score == 4:
story_line.config(text='Will you escape the addiction?')
print('The text is changing again')
score +=1
else:
storyline1()
story_line.config(text='')
#function to clear the first 2 labels once the next button is clicked
def clear_label():
dialogue_1.destroy()
dialogue_2.destroy()
Leave.destroy()
Join.destroy()
def clear_label2():
global dialogue_3
dialogue_3.destroy()
def clear_label3():
dialogue_4.destroy()
def clear_label4():
dialogue_5.pack_forget()
Ignore.pack_forget()
Listen.pack_forget()
def storyline1():
global dialogue_1, dialogue_2, Leave, Join, dialogue_3, dialogue_4, game_Screen, next_button, next_button1
print("We made it")
dialogue_1 = Label(game_screen, text='A thrilling gaming tournament awaits you as you enter a big gaming stadium. The atmosphere is electric, and players from all walks of life are competing fiercely.', wraplength=300)
dialogue_1.pack()
dialogue_2 = Label(game_screen, text='Do you join the tournament?')
dialogue_2.pack()
#join button
Join = Button(game_screen,text="Join", width= 5, height= 2, command=decision_1, font=("Helvetica", 12), anchor=S)
Join.pack()
#Leave button
Leave = Button(game_screen, text="LEAVE", width= 5, height= 2, command=decision_2, font=("Helvetica", 12), anchor=S)
Leave.pack()
#2 different functions to branch the stories
#you join the tournament
def decision_1():
print("You made it to decision 1")
dialogue_3 = Label(game_screen,text="You decide to join the tournament, hooked to the excitement. However, you lose track of time and become deeply engrossed with the games", wraplength=300)
dialogue_3.pack()
#displaying next button
next_button1 = Button(game_screen, text='NEXT', width= 5, height= 2, command=wizard, font=("Helvetica", 12), anchor=S)
next_button1.pack(pady=5)
#removing previous buttons
clear_label()
#you leave the tournament area
def decision_2():
print("You made it to decision 2")
dialogue_4 = Label(game_screen,text="You choose to explore the arena in search of an exit, despite the allure of the competition. After a while of searching, you discover a hidden shortcut that allows you to get around the tournament and continue your adventure.", wraplength=300)
dialogue_4.pack()
#displaying next button
next_button1 = Button(game_screen, text='NEXT', width= 5, height= 2, command=wizard, font=("Helvetica", 12), anchor=S)
next_button1.pack(pady=5)
#removing previous buttons
clear_label()
#both decisions go back to the wizard
def wizard():
dialogue_5 = Label(game_screen, text='As you go about on your adventure you encounter a wise wizard in the gaming arena. He seems eager to share his knowledge about gaming addiction and the importance of balance. Do you...', wraplength=300)
dialogue_5.pack()
Listen = Button(game_screen, text="LISTEN TO THE WISE WIZARD", width= 25, height= 2, command=listen, font=("Helvetica", 12), anchor=S)
Listen.pack()
Ignore = Button(game_screen, text="IGNORE THE WIZARD", width= 18, height= 2, command=ignore, font=("Helvetica", 12), anchor=S)
Ignore.pack()
#clearing previous options
clear_label2()
clear_label3()
def listen():
print("You made it to listening")
dialogue_6 = Label(game_screen, text='You decide to give the Wise Wizard a moment of your time. He teaches you valuable lessons about setting boundaries with tech, managing screen time, and doing other activites.', wraplength=250)
dialogue_6.pack()
showinfo("Ending 1/2 (Good ending)", "You made the right choice listening to the wizard! After your conversation you learn how to maintain a balance between gaming and real-life responsibilites. The door to the exit is shining brightly waiting for your leave.")
#erasing previous Text
clear_label4()
def ignore():
print("Why did u ignore him ;d")
dialogue_7 = Label(game_screen, text='You decide to ignore the Wise Wizard.')
dialogue_7.pack()
showinfo("Ending 2/2 (Bad ending)", "Despite ignoring the wizard's warnings, you become entangled in the gaming tournament, losing track of time and neglecting balance. As a result, the exit is blocked by a chaotic and unsettling environment and crowd as a result you are stuck there forever.")
clear_label4()
#text for story
story_text = [
"Welcome to Escape the Addiction: A Quest for Balance, {}".format(name),
'This is an interactive story game where you will be given',
'a scenario and will have the power to drive the story',
'But be careful, your decisions will result in your ending.',
'Will you escape the addiction?'
]
#next button
next_button = Button(game_screen, text='NEXT', width= 5, height= 2, command=next, font=("Helvetica", 12), anchor=S)
next_button.pack(pady=5)
#actually starting to create the popup text that tells the story
story_line = Label(game_screen, text=story_text[0], font=("Helvetica", 12))
story_line.pack(pady= 5)
game_screen.mainloop()
#creating windows
menu = Canvas(window, width=600, height=400, bg="black")
menu.pack()
#creating background image for intro screen
IntroImg = PhotoImage(file="Intro.png")
Introbg = menu.create_image(0, 0, anchor=NW, image=IntroImg)
#inputting button
start_button = Button(menu, text="START", width= 10, height= 2, command=ask_name, font=("Helvetica", 12), anchor=S)
start_button.place(x=230, y=300)