尝试显示文本框,使用 .pack() 显示,但 .place() 未显示。由于填充,我不想使用 .pack() ;由于relx和rely,我宁愿使用.place()。
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()
尝试使用 .place() 但文本框不会显示,似乎文本正在文本框中写入,因为按钮设置为在文本写入完成后出现。
text_frame.place() 不显示文本框
尝试显示文本框,使用 .pack() 显示,但是 .place() 没有生成。由于填充,我不想使用 .pack() ; 我宁愿使用 .place() 因为 relx 和依赖
问题可以解决。
注释掉
main()
函数中的第32行和33行。
使用
text_frame
作为 Text
小部件。
我为背景和前景添加了颜色。这样,我们就可以确定框架位置了。
片段:
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)
:
:
:
截图: