如何使此文本条目/框架作为一个整体消失?

2024-04-19 13:38:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)消失


1条回答
网友
1楼 · 发布于 2024-04-19 13:38:06

您可以使用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()

相关问题 更多 >