首页 > 解决方案 > 我怎样才能使这个文本条目/框架整体消失?

问题描述

所以我想让我制作的这个框架在按下按钮后消失。继承人的代码:

import tkinter as tk

root = tk.Tk()
root.geometry("150x50+680+350")

def FormSubmission():
    global button_start
    root.attributes("-fullscreen", True)
    frame = tk.Frame(root)
    tk.Label(frame, text="First Name:").grid(row=0)
    entry1 = tk.Entry(frame)
    entry1.grid(row=0, column=1)
    tk.Label(frame, text="Last Name:").grid(row=1)
    e2 = tk.Entry(frame)
    e2.grid(row=1, column=1)
    tk.Label(frame, text="Email:").grid(row=2)
    e3 = tk.Entry(frame)
    e3.grid(row=2, column=1)
    tk.Label(frame, text="Date of Birth:").grid(row=3)
    e4 = tk.Entry(frame)
    e4.grid(row=3, column=1)
    frame.pack(anchor='center', expand=True)
    button_next = tk.Button(root, text = "Next", height = 2, width = 7, command = MainPage).pack()
    button_start.place_forget()

def MainPage():
    global FormSubmission
    root.attributes("-fullscreen", True)
    l1 = tk.Label(root, text = "Welcome Back,"   , font = ("Arial", 44)).pack()
    button_start.place_forget() # You can also use `button_start.destroy()`






button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)

root.mainloop()

如您所见,我想让输入字段的框架/功能(FormSubmission)在功能(MainPage)中按下下一个按钮后消失。

标签: pythonuser-interfacetkinterframe

解决方案


您可以使用pack_forget

每个几何管理器(包、网格、位置)都有自己的..._forget

我已经为您希望的用例重新编辑了您的代码段。

无论如何,我认为您应该重新设计您的应用程序。

import tkinter as tk

root = tk.Tk()
root.geometry("150x50+680+350")

def FormSubmission():
    global button_start
    root.attributes("-fullscreen", True)
    frame = tk.Frame(root)
    tk.Label(frame, text="First Name:").grid(row=0)
    entry1 = tk.Entry(frame)
    entry1.grid(row=0, column=1)
    tk.Label(frame, text="Last Name:").grid(row=1)
    e2 = tk.Entry(frame)
    e2.grid(row=1, column=1)
    tk.Label(frame, text="Email:").grid(row=2)
    e3 = tk.Entry(frame)
    e3.grid(row=2, column=1)
    tk.Label(frame, text="Date of Birth:").grid(row=3)
    e4 = tk.Entry(frame)
    e4.grid(row=3, column=1)
    frame.pack(anchor='center', expand=True)
    button_next = tk.Button(root, text = "Next", height = 2, width = 7, command = lambda: MainPage(frame)).pack()
    button_start.place_forget()

def MainPage(frame):
    global FormSubmission
    frame.pack_forget()
    root.attributes("-fullscreen", True)
    l1 = tk.Label(root, text = "Welcome Back,"   , font = ("Arial", 44)).pack()
    button_start.place_forget() # You can also use `button_start.destroy()`

button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)

root.mainloop()

推荐阅读