为什么 tkinter 多个 window GUI 无法正常工作?

Why tkinter multiple window GUI not working properly?

我正在使用 tkinter 创建多个 window GUI。 我没有收到任何错误。第一个 window 运行 成功但是当我点击按钮时第二个 window 没有 运行.

我不明白哪里有错误, 如果我打包 Entry 小部件和 Buttons 它们不会显示在 window.

import tkinter as tk
import pywhatkit
import pyautogui


class automation(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("842x510")
        self.configure(bg="#ffffff")
        self.resizable(False, False)

        self.container = container = tk.Frame(self, background="#ffffff")
        container.grid(row=0, column=0, sticky='nesw')
        container.configure(background="#ffffff")
        container.pack(side="top", fill="both")

        self.frames = {}

        for F in (MainWindow, SecondWindow):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            frame.grid(row=0, column=0, sticky="nsew")
            self.frames[page_name] = frame

        self.show_frame('MainWindow')

    def show_frame(self, page_name):
        if page_name not in self.frames:
            self.frames[page_name] = page_name(self.container, self)

        frame = self.frames[page_name]
        frame.tkraise()


def btn_clicked():
    print("Button Clicked")

第一个代码 window。

class MainWindow(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.frame = tk.Frame(self, background="#ffffff")
        self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=391, bd=0, 
        highlightthickness=0, relief="ridge")
        self.canvas.place(x=0, y=0)
        self.canvas.pack(side='left')
        self.background_img = tk.PhotoImage(file="background.png")
        self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")

        self.img1 = tk.PhotoImage(file="img1.png")
        self.b1 = tk.Button(self, image=self.img1, borderwidth=0, highlightthickness=0,
                            command=lambda: controller.show_frame(SecondWindow),
                            relief="flat")

        self.b1.place(x=392, y=100, width=348, height=62)
        self.b1.pack(padx=10, pady=125)
        self.b1.pack()

        # creating third button

        self.img3 = tk.PhotoImage(file="img3.png")
        self.b3 = tk.Button(self, image=self.img3, borderwidth=0, highlightthickness=0, 
                            command=btn_clicked, relief="flat")

        self.b3.place(x=392, y=200, width=348, height=62)
        self.b3.pack(padx=10, pady=0)
        self.b3.pack()
        self.canvas.pack()

def send(num, msg, hour, minute):
    pywhatkit.sendwhatmsg(f"+91{num}", msg, hour, minute, 36)
    print("hello")
    pyautogui.click()
    pyautogui.press("enter")

第二个代码 window:

class SecondWindow(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        a = tk.StringVar()
        b = tk.StringVar()
        c = tk.IntVar()
        d = tk.IntVar()
        self.controller = controller
        self.frame = tk.Frame(self, background="#ffffff")
        self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=842, bd=0, 
                                highlightthickness=0, relief="ridge")
        self.canvas.place(x=0, y=0)
        self.canvas.pack(side='left')

        # set image in background
        self.background_img = tk.PhotoImage(file="background2.png")
        self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")

        # enter a number
        self.entry1_img = tk.PhotoImage(file="textBox1.png")
        self.canvas.create_image(570, 112, image=self.entry1_img)

        self.entry1 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=a)
        self.entry1.place(x=423, y=90, width=280, height=45)
        #self.entry1.pack()

        # enter a massage

        self.entry2_img = tk.PhotoImage(file="textBox2.png")
        self.canvas.create_image(570, 225, image=self.entry2_img)

        self.entry2 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=b)

        self.entry2.place(x=423, y=203, width=280, height=45)
        #self.entry2.pack()

        # enter a time -> Hour
        self.entry3_img = tk.PhotoImage(file="textBox3.png")
        self.canvas.create_image(470, 329, image=self.entry3_img)

        self.entry3 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=c)

        self.entry3.place(x=423, y=312, width=80.0, height=35)
        #self.entry3.pack()

        # minute
        self.entry4_img = tk.PhotoImage(file="textBox4.png")
        self.canvas.create_image(676, 329, image=self.entry4_img)

        self.entry4 = tk.Entry(self, bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=d)

        self.entry4.place(x=630, y=312, width=80.0, height=35)
        #self.entry4.pack()

        # Go home
        self.img4 = tk.PhotoImage(file="img4.png")
        self.b4 = tk.Button(self, image=self.img4, borderwidth=0, highlightthickness=0,
                            command=lambda: controller.show_frame(MainWindow), relief="flat")

        self.b4.place(x=418, y=400, width=100, height=37)
        #self.b4.pack()

        # Send message
        self.img5 = tk.PhotoImage(file="img5.png")
        self.b5 = tk.Button(self, image=self.img5, borderwidth=0, highlightthickness=0,
                            command=lambda: send(a.get(), b.get(), c.get(), d.get()), 
                            relief="flat")

        self.b5.place(x=642, y=400, width=100, height=37)
        #self.b5.pack()
        self.canvas.pack()


if __name__ == '__main__':
    app = automation()
    app.mainloop()

command=lambda: controller.show_frame(SecondWindow), 将 class id 作为参数传递,因此未找到 page_name

建议

command=lambda: controller.show_frame('SecondWindow')