简体   繁体   English

在tkinter中创建后退按钮?

[英]Creating a back button in tkinter?

I have a button, encrypt_button , which destroys what was originally on the screen and displays something new under def encrypt() . 我有一个按钮encrypt_button ,它破坏了屏幕上的原始内容,并在def encrypt() encrypt_button def encrypt()下显示了一些新内容。 I want to create a back button which can restore what was on a previous screen after it has been destroyed. 我想创建一个后退按钮,该按钮可以在销毁之前的屏幕上还原之前的屏幕。 I have tried the following which does not destroy what is under def encrypt() and if you click the button again, the original labels will not be destroyed either. 我尝试了以下操作,但不会破坏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()

I have also tried without defining each label and button but that doesn't work either. 我也尝试过不定义每个标签和按钮,但这也不起作用。 (Obviously I'd get the x is not defined error) (显然我会得到x is not defined错误)

The problem in your code is that all of your variables are local variables, so they are only visible in the function that creates them. 代码中的问题是所有变量都是局部变量,因此它们仅在创建它们的函数中可见。

The simplest solution is to make each "page" be a frame. 最简单的解决方案是使每个“页面”成为一个框架。 Make sure the handle to that frame is global, or an instance attribute if you're using classes. 确保该帧的句柄是全局的,如果使用的是类,则确保为实例属性。

Once you have that, simply delete the frame for the current page and recreate the frame for the other page. 完成后,只需删除当前页面的框架,然后为另一页面重新创建框架。 When you delete the frame, any children will also automatically get deleted. 删除框架时,所有子项也将自动被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM