繁体   English   中英

如何在 python 中使用 Tkinter 创建后退按钮

[英]How to create back button with Tkinter in python

我正在尝试创建一个对我已实现的按钮做出反应的 GUI。

预期 output:按下“返回”按钮将恢复第一个 window。 [主窗口]

目前 output:按“返回”按钮将回调 function [删除辅助窗口的按钮并调用原始窗口] 但不会恢复原始 Z05B8C74CBD96FBF2DE4C1A352702FFF4Z

这是我的实现:

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

从我在 def back() function 中可以看到,您没有破坏 window,而是调用了按钮。

在 def Second_window() function 中,您将 GUI 命名为与主窗口相同的名称,这将在尝试调用其中任何一个时导致错误,因为代码将不知道要恢复哪个 window。 将第二个 window 更改为 Second_window 将有助于解决此问题

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

使用框架或为每个按钮创建一个新的 window 可能会有所帮助并使导航更容易。

暂无
暂无

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

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