#Problems with executing two frames

9 messages · Page 1 of 1 (latest)

wintry crown
#

import tkinter as tk
import random

root = tk.Tk()
root.title("The Dice Game")
root.geometry("500x500")

root.configure(bg="#FFFFE0")

purple_block1 = tk.Frame(root, bg="purple", width=450, height=450)
purple_block1.pack(pady=10)
purple_block1.pack_propagate(False)

inside_text = tk.Label(
purple_block1,
text="Please, enter your name \nto start the game (〃^ー^〃)",
font=("Arial", 15, "bold"),
fg="#FFFFE0",
bg="purple"
)
inside_text.pack(pady=10)

name_text = tk.Label(
purple_block1,
text="Enter Name :",
font=("Times New Roman", 30, "bold"),
fg="#FFFFE0",
bg="purple"
)
name_text.pack(pady=1)

entry = tk.Entry(purple_block1)
entry.pack()

purple_block2 = tk.Frame(root, bg="purple", width=450, height=450)
purple_block2.pack(pady=10)
purple_block2.pack_propagate(False)

def go_to_loading():
name = entry.get()

purple_block1.pack_forget()
purple_block1.pack(fill="both", expand=True)

fra2load = tk.Label(
purple_block2,
text="Loading {name}...",
font=("Arial", 15, "bold"),
fg="#FFFFE0",
bg="purple"

)
fra2load.pack(pady=10, expand=True)

purple_block2 = tk.Frame(root, bg="purple", width=450, height=450)
purple_block2.pack(pady=10)
purple_block2.pack_propagate(False)

text_label = tk.Label(
root,
text="⋆ ꩜ The Dice Game ꩜ ⋆",
font=("Times New Roman", 30, "bold"),
fg="purple",
bg="#FFFFE0"
)
text_label.pack(pady=3)

button = tk.Button(
purple_block1,
text="Submit",
command=go_to_loading
)
button.pack(pady=10)

root.mainloop()

rustic sunBOT
#

Hi leo! Thanks for sharing your code. I can see you're working with tkinter frames, but I need a bit more information to help you effectively.

Please clarify:

  • What specific problem are you experiencing? (e.g., frames not displaying, error message, unexpected behavior)
  • What error message do you see, if any?
  • What's the expected behavior vs. what's actually happening?
  • Have you tried any troubleshooting steps?

Once you provide these details, someone can give you a precise answer! 😊

#

-# Python @sly glade @deft zodiac @indigo yarrow @tender basin

wintry crown
tender basin
#

You probably want each screen to be a separate frame
and then hide the old frame and then show the new frame

#

purple_block2 is created twice it seems and then packed immediately?

tender basin
rustic sunBOT
#

-# Writing a response for "Fixing tkinter frame switching and NameError"

rustic sunBOT
# tender basin <@1368557938507255959> can you please point out issues with this code?

Hey! Frame switching in tkinter can be tricky. The best way to handle this is to use a controller class so you don't run into those pesky NameErrors.

Basically, you create a main Tk class that holds a dictionary of your frames. Instead of destroying frames, you just use frame.tkraise() to bring the one you want to the front. This keeps everything scoped correctly and avoids global variable issues.