Attempting to display a text box, was displaying with .pack() but .place() isn't making. I don't want to use .pack() because of padding; I'd rather .place() because of relx and rely.
import tkinter as tk
from tkinter import messagebox
import time
def typewriter_effect(text_widget, text, buttons):
for button in buttons:
button.place_forget() # Hide buttons while typing
for char in text:
text_widget.insert(tk.END, char)
text_widget.see(tk.END)
text_widget.update()
time.sleep(0.05)
for button in buttons:
button.place(relx=0.5, rely=0.5, anchor=tk.CENTER) # Show buttons after typing
def explore_room():
messagebox.showinfo("Exploration", "You explored the room. You found a key!")
def go_back_to_sleep():
messagebox.showinfo("Back to Sleep", "You went back to sleep. Zzz...")
def look_for_light_switch():
messagebox.showinfo("Light Switch", "You found the light switch. The room is now illuminated.")
def main():
root = tk.Tk()
root.title("Text Adventure Game")
root.attributes("-fullscreen", True)
text_options_frame = tk.Frame(root, bd=2, relief=tk.RIDGE)
text_options_frame.pack(expand=True)
text_frame = tk.Frame(text_options_frame, bd=0)
text_frame.place(relx=0.5, rely=0.7, anchor=tk.CENTER) # Placing at the bottom center
text_scroll = tk.Scrollbar(text_frame)
text_scroll.pack(side=tk.RIGHT, fill=tk.Y)
text_widget = tk.Text(text_frame, wrap=tk.WORD, yscrollcommand=text_scroll.set, height=10, width=120, font=("Helvetica", 12))
text_widget.pack(fill=tk.BOTH, expand=True)
text_scroll.config(command=text_widget.yview)
button_frame = tk.Frame(root)
button_frame.pack(side=tk.BOTTOM, pady=10)
explore_button = tk.Button(button_frame, text="Explore the room", command=explore_room)
sleep_button = tk.Button(button_frame, text="Go back to sleep", command=go_back_to_sleep)
light_button = tk.Button(button_frame, text="Look for a light switch", command=look_for_light_switch)
typewriter_effect(text_widget, "Welcome to the Text Adventure Game!\n\n"
"You find yourself in a dark room. What would you like to do?\n",
[explore_button, sleep_button, light_button])
delay([explore_button, sleep_button, light_button], 1)
explore_button.pack(side=tk.LEFT, padx=5)
sleep_button.pack(side=tk.LEFT, padx=5)
light_button.pack(side=tk.LEFT, padx=5)
delay([explore_button], 1)
explore_button.pack_forget()
root.mainloop()
def delay(button, wait):
for widget in button:
widget.update()
time.sleep(wait)
if __name__ == "__main__":
main()
Tried using .place() but text box won't display, seems like the text is writing in the text box because the buttons are set to appear after the text is finished writing.
text_frame.place() Not displaying text box
Attempting to display a text box, was displaying with .pack() but .place() isn't making. I don't want to use .pack() because of padding; I'd rather .place() because of relx and rely
The problem can be fixed.
Comment out on line 32 and 33 in main()
function.
Used the text_frame
for Text
widget.
I added color to the background and foreground. So, we can determine the frame position.
Snippet:
def main():
root = tk.Tk()
root.title("Text Adventure Game")
root.attributes("-fullscreen", True)
#text_options_frame = tk.Frame(root, bd=2, relief=tk.RIDGE)
#text_options_frame.pack(expand=True)
text_frame = tk.Frame(root, bd=2, relief=tk.RIDGE)
text_frame.place(relx=0.63, rely=0.7, anchor=tk.CENTER) # Placing at the bottom center
text_scroll = tk.Scrollbar(text_frame)
text_scroll.pack(side=tk.RIGHT, fill=tk.Y)
text_widget = tk.Text(text_frame, wrap=tk.WORD, bg='blue', fg='white', yscrollcommand=text_scroll.set, height=10, width=120, font=("Helvetica", 12))
text_widget.pack(fill=tk.BOTH, expand=True)
:
:
:
Screenshot: