在 tkinter 中创建后退按钮?
Creating a back button in tkinter?
我有一个按钮 encrypt_button
,它会破坏屏幕上原来的内容并在 def encrypt()
下显示新内容。我想创建一个后退按钮,它可以在它被销毁后恢复上一个屏幕上的内容。我已经尝试了以下不会破坏 def encrypt()
下的内容的方法,如果您再次单击该按钮,原始标签也不会被破坏。
import tkinter
window = tkinter.Tk()
entry = tkinter.Entry(window)
def encrypt():
encrypt_label = tkinter.Label(window, text="Please enter the message you'd like to encrypt", font=('Helvetica', 14))
encrypt_label.pack()
entry = tkinter.Entry(window)
entry.pack()
encrypt_confirm = tkinter.Button(window, text="Confirm")
encrypt_confirm.pack()
back_button = tkinter.Button(window, text="Back", command=back)
back_button.pack()
encrypt_button.destroy()
title_header.destroy()
title_label.destroy()
heading_label.destroy()
def back():
title_header = tkinter.Label(window, text="ENCRYPTION/DECRYPTION", font=('Helvetica', 16))
title_header.pack()
title_label = tkinter.Label(window, text="Welcome to this encryption and decryption program")
title_label.pack()
heading_label = tkinter.Label(window, text="When you are ready, press a button to continue")
heading_label.pack()
encrypt_button = tkinter.Button(window, text="Encrypt", command=encrypt)
encrypt_button.pack()
encrypt_label.destroy()
entry.destroy()
encrypt_confirm.destroy()
back_button.destroy()
title_header = tkinter.Label(window, text="ENCRYPTION/DECRYPTION", font=('Helvetica', 16))
title_header.pack()
title_label = tkinter.Label(window, text="Welcome to this encryption and decryption program")
title_label.pack()
heading_label = tkinter.Label(window, text="When you are ready, press a button to continue")
heading_label.pack()
encrypt_button = tkinter.Button(window, text="Encrypt", command=encrypt)
encrypt_button.pack()
encrypt_label = tkinter.Label(window, text="Please enter the message you'd like to encrypt", font=('Helvetica', 14))
entry = tkinter.Entry(window)
encrypt_confirm = tkinter.Button(window, text="Confirm")
back_button = tkinter.Button(window, text="Back", command=back)
window.mainloop()
我也尝试过不定义每个标签和按钮,但这也不起作用。 (显然我会得到 x is not defined
错误)
您代码中的问题是您的所有变量都是局部变量,因此它们仅在创建它们的函数中可见。
最简单的解决方案是让每个 "page" 成为一个框架。确保该框架的句柄是全局句柄,或者如果您使用 类,则为实例属性。
完成后,只需删除当前页面的框架并为其他页面重新创建框架。当您删除框架时,所有子项也将自动删除。
我有一个按钮 encrypt_button
,它会破坏屏幕上原来的内容并在 def encrypt()
下显示新内容。我想创建一个后退按钮,它可以在它被销毁后恢复上一个屏幕上的内容。我已经尝试了以下不会破坏 def encrypt()
下的内容的方法,如果您再次单击该按钮,原始标签也不会被破坏。
import tkinter
window = tkinter.Tk()
entry = tkinter.Entry(window)
def encrypt():
encrypt_label = tkinter.Label(window, text="Please enter the message you'd like to encrypt", font=('Helvetica', 14))
encrypt_label.pack()
entry = tkinter.Entry(window)
entry.pack()
encrypt_confirm = tkinter.Button(window, text="Confirm")
encrypt_confirm.pack()
back_button = tkinter.Button(window, text="Back", command=back)
back_button.pack()
encrypt_button.destroy()
title_header.destroy()
title_label.destroy()
heading_label.destroy()
def back():
title_header = tkinter.Label(window, text="ENCRYPTION/DECRYPTION", font=('Helvetica', 16))
title_header.pack()
title_label = tkinter.Label(window, text="Welcome to this encryption and decryption program")
title_label.pack()
heading_label = tkinter.Label(window, text="When you are ready, press a button to continue")
heading_label.pack()
encrypt_button = tkinter.Button(window, text="Encrypt", command=encrypt)
encrypt_button.pack()
encrypt_label.destroy()
entry.destroy()
encrypt_confirm.destroy()
back_button.destroy()
title_header = tkinter.Label(window, text="ENCRYPTION/DECRYPTION", font=('Helvetica', 16))
title_header.pack()
title_label = tkinter.Label(window, text="Welcome to this encryption and decryption program")
title_label.pack()
heading_label = tkinter.Label(window, text="When you are ready, press a button to continue")
heading_label.pack()
encrypt_button = tkinter.Button(window, text="Encrypt", command=encrypt)
encrypt_button.pack()
encrypt_label = tkinter.Label(window, text="Please enter the message you'd like to encrypt", font=('Helvetica', 14))
entry = tkinter.Entry(window)
encrypt_confirm = tkinter.Button(window, text="Confirm")
back_button = tkinter.Button(window, text="Back", command=back)
window.mainloop()
我也尝试过不定义每个标签和按钮,但这也不起作用。 (显然我会得到 x is not defined
错误)
您代码中的问题是您的所有变量都是局部变量,因此它们仅在创建它们的函数中可见。
最简单的解决方案是让每个 "page" 成为一个框架。确保该框架的句柄是全局句柄,或者如果您使用 类,则为实例属性。
完成后,只需删除当前页面的框架并为其他页面重新创建框架。当您删除框架时,所有子项也将自动删除。