简体   繁体   中英

How to create back button with Tkinter in python

I'm trying to create a GUI that reacting to the buttons that I've implemented.

Expected output: pushing "back" button will restore the first window. [main window]

Currently output: pushing "back" button will call back function [which removing the secondary window's buttons and call the original window] but not restoring the original window

This is my implementation:

import tkinter
from tkinter import *
from PIL import ImageTk, Image

applied_logo_path = r'C:\Users\e174701\OneDrive - Applied Materials\Desktop\amatlogo.png'
logo_path = r'C:\Users\e174701\OneDrive - Applied Materials\Desktop\amatmain.png'

font1 = ('Helvetica 15', 20)
# -----------------------------------------------------------------------------------------------------


def main_window():

    def Second_window():
        def back():
            button_previous.destroy()
            button_execute.destroy()
            main_window()
        # Second window: [User pressed "next"]

        main_window.title('MC installer')
        main_window.geometry("800x600")

        # Presenting Applied Materials logo:
        main_window_logo = ImageTk.PhotoImage(Image.open(logo_path))
        main_window_logo_label = Label(image=main_window_logo, width=800, height=300)
        main_window_logo_label.place(x=5, y=100)
        button_next.destroy()
        button_readME.destroy()
        # execute button: [will start the process]
        pixel = tkinter.PhotoImage(width=1, height=1)
        button_execute = Button(main_window, text="Execute", image=pixel, width=80, height=30, compound="c")
        button_execute.place(x=700, y=550)

        # previous button: [will return the user to the main window]
        button_previous = Button(main_window, text="Previous", image=pixel, width=80, height=30, compound="c",
                                 command=lambda: back())
        button_previous.place(x=10, y=550)
        main_window.mainloop()

    # main window general definitions:
    main_window = Tk()

    main_window.title('MC installer')
    # instead this comment need to define the upper toolbar logo.
    main_window.geometry("800x600")

    # Presenting Applied Materials logo:
    main_window_logo = ImageTk.PhotoImage(Image.open(logo_path))
    main_window_logo_label = Label(image=main_window_logo, width=800, height=300)
    main_window_logo_label.place(x=5, y=100)

    # next button: [will take the user to the next window]
    pixel = tkinter.PhotoImage(width=1, height=1)
    button_next = Button(main_window, text="Next", image=pixel, width=80, height=30, compound="c", command=lambda: Second_window())
    button_next.place(x=700, y=550)

    # exit button: [will close the installer.]
    button_exit = Button(main_window, text="Exit", image=pixel, width=80, height=30, compound="c", command=lambda: main_window.quit())
    button_exit.place(x=10, y=550)

    # read me button: [will guide the user]
    button_readME = Button(main_window, text="Read me", image=pixel, width=80, height=30, compound="c")
    button_readME.place(x=345, y=550)
    main_window.mainloop()

From what I can see in the def back() function, you are not destroying the window instead you are calling the button.

Inside the def Second_window() function you gave the GUI the same name as the main one, this will cause errors when trying to call anyone of them as the code will not know which window to restore. Changing the second window to Second_window will help with this problem

def back():
            Second_window.destroy()
            main_window()

Using frames or creating a new window for each button might help and make the navigation easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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