You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter中销毁Frame并恢复初始布局?

问题描述

输入图片描述

在下方代码中,我销毁了frame1和frame2,以便消息能利用销毁这两个框架后腾出的额外空间。

需要实现的功能

  • 发送消息时,frame1和frame2需自动销毁。
  • 消息要利用腾出的额外空间,但必须位于new_note_button下方。
  • 点击new_note_button时,需清空所有消息,并让frame1、frame2恢复初始状态。

我尝试在点击new_note_button时重新创建frame1和frame2,但框架位置不对且会生成多个框架(可查看我附上的图片)。

我的代码

import tkinter as tk
from tkinter import ttk

frames_loaded = True

def clear_chat_log():
    global frames_loaded
    if not frames_loaded:
        # Create frames to group the boxes
        frame1 = tk.Frame(root, bg="white")
        frame1.pack(side=tk.TOP, padx=(0, 40), pady=(100, 1))

        frame2 = tk.Frame(root, bg="white")
        frame2.pack(side=tk.TOP, padx=(0, 40), pady=(5, 20))

        style.configure("PDF.TButton", font=('Sans-serif', 12,), background="white", fg="black", highlightthickness=1, highlightbackground="gray", borderwidth=1)
        chat_with_pdf_button = ttk.Button(frame1, text="Teyxxxxxxxxxxxx", style="PDF.TButton")
        chat_with_pdf_button.pack(side=tk.LEFT, ipadx=(135), padx=(0, 10), ipady=14)

        option2_label = tk.Label(frame1, text="testxxxxxxxxxxxxxxxxx", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=95, pady=20)
        option2_label.pack(side=tk.LEFT)

        # Create labels for the options in the second frame
        option3_label = tk.Label(frame2, text="testzzzzzzzzzzzzzzzz", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=116, pady=20)
        option3_label.pack(side=tk.LEFT, padx=(0, 10))

        option4_label = tk.Label(frame2, text="testqqqqqqqqqqqqqqqqq", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=115, pady=20)
        option4_label.pack(side=tk.LEFT)
        frames_loaded = True
        return

root = tk.Tk()
# Adjust the placement of other widgets accordingly
root.title("Test")
# Maximize the window
root.attributes('-zoomed', True)
style = ttk.Style()
style.theme_use("clam")
chat_frame = tk.Frame(root, bg="white")
chat_frame.pack(expand=True, fill=tk.BOTH)

chat_log = tk.Text(chat_frame, state='disabled', wrap='word', width=70, height=15, font=('Sans-serif', 12), bg="white", fg="black", highlightthickness=0, borderwidth=0)
chat_log.pack(side=tk.LEFT, padx=(500,0), pady=10, expand = True, fill = tk.BOTH)

# Create frames to group the boxes
frame1 = tk.Frame(root, bg="white")
frame1.pack(side=tk.TOP, padx=(0, 40), pady=(100, 1))

frame2 = tk.Frame(root, bg="white")
frame2.pack(side=tk.TOP, padx=(0, 40), pady=(5, 20))

style.configure("PDF.TButton", font=('Sans-serif', 12,), background="white", fg="black", highlightthickness=1, highlightbackground="gray", borderwidth=1)
chat_with_pdf_button = ttk.Button(frame1, text="Teyxxxxxxxxxxxx", style="PDF.TButton")
chat_with_pdf_button.pack(side=tk.LEFT, ipadx=(135), padx=(0, 10), ipady=14)

option2_label = tk.Label(frame1, text="testxxxxxxxxxxxxxxxxx", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=95, pady=20)
option2_label.pack(side=tk.LEFT)

# Create labels for the options in the second frame
option3_label = tk.Label(frame2, text="testzzzzzzzzzzzzzzzz", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=116, pady=20)
option3_label.pack(side=tk.LEFT, padx=(0, 10))

option4_label = tk.Label(frame2, text="testqqqqqqqqqqqqqqqqq", font=('Sans-serif', 12,), bg="white", fg="black", highlightthickness=1, highlightbackground="gray", padx=115, pady=20)
option4_label.pack(side=tk.LEFT)

def append_to_chat_log(sender=None, message=None):
  
    chat_log.config(state=tk.NORMAL)
    if sender:
        chat_log.insert("end", f"{sender}\n\n", "sender")
        
        #chat_log.insert("end",'\n\n')
    if message:
        chat_log.insert("end", message)
    chat_log.tag_config("sender", font=('Arial', 12, 'bold'), foreground="black")
    chat_log.config(state=tk.DISABLED)
    chat_log.see("end")
    chat_log.update()

def send_message(event=None):
    global frames_loaded
    if frame1:
        frame1.destroy()
    if frame2:
        frame2.destroy()
    frames_loaded = False
    message = message_entry.get(1.0, "end-1c") 
    message = message.strip()
    message_entry.delete(1.0, tk.END)
    message_entry.update()
    
    if not message:
        pass 
    else:
        canvas1.place(x=495,y=80)
        canvas1.update()
        
        append_to_chat_log("User")
        append_to_chat_log(message=message)
        append_to_chat_log(message="\n")
        canvas1.place_forget()
        canvas1.update()

new_note_button = ttk.Button(chat_frame, style="Toggle.TButton", text=" New Note", compound="left", command=clear_chat_log)
new_note_button.place(x=490,y=40)

canvas1 = tk.Canvas(root, width=250, height=70, bg="white", borderwidth=0, highlightthickness=0)
# Display "Loading" text
loading_text = canvas1.create_text(70, 50, text="Loading...", anchor="e")

message_entry = tk.Text(root, padx=17, insertbackground='white', bg="white", fg="black", width=70, height=1, spacing1=20, spacing3=20, font=('Open Sans', 14))
message_entry.pack(side=tk.LEFT, padx=(500, 0), pady=(0, 70))  # Adjust pady to move it slightly above the bottom
#message_entry.insert(0, "Ask me anything...")
message_entry.insert(1.0, "Type")
message_entry.mark_set("insert", "%d.%d" % (0,0))
message_entry.bind("<Return>", send_message)
root.mainloop()

内容的提问来源于stack exchange,提问作者noocoder777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:23:23