【问题标题】:How can i make this text entry/frame as a whole disappear?我怎样才能使这个文本条目/框架整体消失?
【发布时间】:2021-06-15 15:06:57
【问题描述】:

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

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)中按下下一个按钮后消失。

【问题讨论】:

    标签: python user-interface tkinter frame


    【解决方案1】:

    你可以使用pack_forget

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

    我已针对您希望的用例重新编辑了您的 sn-p。

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

    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()
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
    • 我详细阐述了我的答案。
    猜你喜欢
    • 2010-11-21
    • 2015-11-12
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多