放置在 window 中时,tkinter 按钮无法正常工作或显示图像

tkinter buttons not functioning or showing image when placed in window

所以我对在 Python 中编写面向对象的代码还很陌生,对制作 GUI 也很陌生。我需要帮助来理解为什么以下内容不在按钮上显示任何图像以及为什么按钮不起作用但顶部菜单工作正常:

def callback():
    print("click!")


class Window(Frame):

# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
    # parameters that you want to send through the Frame class.
    Frame.__init__(self, master)

    # reference to the master widget, which is the tk window
    self.master = master

    # with that, we want to then run init_window, which doesn't yet exist
    self.init_window()


def __init__(self, master=None):
    # parameters that you want to send through the Frame class.
    Frame.__init__(self, master)

    # reference to the master widget, which is the tk window
    self.master = master

    # with that, we want to then run init_window, which doesn't yet exist
    self.init_window()

# Creation of init_window
def init_window(self):

    self.master.title("ABC Automation Platform")
    p1 = IdsPage(self)

    self.grid()

    # creating a menu instance
    menu = Menu(self)
    self.master.config(menu=menu)

    # create the file object)
    file = Menu(menu, tearoff=False)
    file.add_command(label="Exit", command=client_exit)

    file.add_command(label="Download All", command=download_all)
    file.add_command(label="Rename All", command=rename_all)
    menu.add_cascade(label="File", menu=file)

    edit = Menu(menu, tearoff=False)
    help = Menu(menu, tearoff=False)
    help.add_command(label="Help")
    edit.add_command(label="Undo")
    menu.add_cascade(label="Edit", menu=edit)
    menu.add_cascade(label="Help", menu=help)

    btn_paths = "Resources/Buttons/"
    img_ids = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_sox = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_sps = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_dev = PhotoImage(file=btn_paths + "btn_btn_1.png")

    b_ids = Button(self, height=150, width=150, image=img_ids, command=callback)
    b_ids.grid(row=1, column=1, padx=(70, 50), pady=10)

    b_sox = Button(self, height=150, width=150, image=img_sox, command=callback)
    b_sox.grid(row=1, column=2, pady=10)

    b_sps = Button(self, height=150, width=150, image=img_sps, command=callback)
    b_sps.grid(row=2, column=1, padx=(70, 50), pady=5)

    b_dev = Button(self, height=150, width=150, image=img_dev, command=callback)
    b_dev.grid(row=2, column=2, pady=5)



if __name__ == '__main__':
    root = Tk()
    app = Window(root)
    root.grid()
    root.geometry("500x350")
    root.mainloop()

给出这个输出:

[

顶部菜单工作正常,但按钮没有任何作用,按钮上的图像也没有显示。

虽然如果我将按钮的代码移动到 main 方法中(对于 if name == '[= 这是否是 python 中的正确名称33=]main': 部分 ?), 它开始工作。

如果代码是:

# Creation of init_window
def init_window(self):
    # changing the title of our master widget
    self.master.title("ABC Automation Platform")
    p1 = IdsPage(self)
    # allowing the widget to take the full space of the root window
    # self.pack(fill=BOTH, expand=1)
    self.grid()

    # creating a menu instance
    menu = Menu(self)
    #self.master.config(menu=menu)

    # create the file object)
    file = Menu(menu, tearoff=False)
    file.add_command(label="Exit", command=client_exit)


    file.add_command(label="Download All", command=download_all)
    file.add_command(label="Rename All", command=rename_all)

    menu.add_cascade(label="File", menu=file)

    edit = Menu(menu, tearoff=False)
    help = Menu(menu, tearoff=False)
    help.add_command(label="Help")
    edit.add_command(label="Undo")
    menu.add_cascade(label="Edit", menu=edit)
    menu.add_cascade(label="Help", menu=help)

    self.master.config(menu=menu)
# root window created. Here, that would be the only window, but
# you can later have windows within windows.


if __name__ == '__main__':
    root = Tk()

    btn_paths = "Resources/Buttons/"
    img_ids = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_sox = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_sps = PhotoImage(file=btn_paths + "btn_btn_1.png")
    img_dev = PhotoImage(file=btn_paths + "btn_btn_1.png")
    # body = Frame(root)
    b_ids = Button(root, height=150, width=150, image=img_ids, command=callback)
    b_ids.grid(row=1, column=1, padx=(70, 50), pady=10)

    b_sox = Button(root, height=150, width=150, image=img_sox, command=callback)
    b_sox.grid(row=1, column=2, pady=10)

    b_sps = Button(root, height=150, width=150, image=img_sps, command=callback)
    b_sps.grid(row=2, column=1, padx=(70, 50), pady=5)

    b_dev = Button(root, height=150, width=150, image=img_dev, command=callback)
    b_dev.grid(row=2, column=2, pady=5)

    # creation of an instance
    app = Window(root)
    root.grid()
    root.geometry("500x350")
    # mainloop
    root.mainloop()

一切开始正常工作,如下所示:

点击按钮也会执行它们应该执行的操作(在本例中只是打印 "click")。我的理解是有限的,但这并不理想,我想在 window class 而不是 "main method" 中初始化我的按钮。谁能帮我弄清楚为什么会这样?

创建按钮时,需要保留图片,否则会被垃圾回收器销毁。

我没有你的图片,但我的可以用

def callback():
    print("click!")


class Window(Frame):

    # Define settings upon initialization. Here you can specify
    def __init__(self, master=None):
        # parameters that you want to send through the Frame class.
        Frame.__init__(self, master)

        # reference to the master widget, which is the tk window
        self.master = master

        # with that, we want to then run init_window, which doesn't yet exist
        self.init_window()



    # Creation of init_window
    def init_window(self):

        self.master.title("ABC Automation Platform")
        p1 = IdsPage(self)

        self.grid()

        # creating a menu instance
        menu = Menu(self)
        self.master.config(menu=menu)

        # create the file object)
        file = Menu(menu, tearoff=False)
        file.add_command(label="Exit", command=client_exit)

        file.add_command(label="Download All", command=download_all)
        file.add_command(label="Rename All", command=rename_all)
        menu.add_cascade(label="File", menu=file)

        edit = Menu(menu, tearoff=False)
        help = Menu(menu, tearoff=False)
        help.add_command(label="Help")
        edit.add_command(label="Undo")
        menu.add_cascade(label="Edit", menu=edit)
        menu.add_cascade(label="Help", menu=help)

        btn_paths = "Resources/Buttons/"
        self.img_ids = PhotoImage(file=btn_paths + "btn_btn_1.png")
        self.img_sox = PhotoImage(file=btn_paths + "btn_btn_1.png")
        self.img_sps = PhotoImage(file=btn_paths + "btn_btn_1.png")
        self.img_dev = PhotoImage(file=btn_paths + "btn_btn_1.png")

        self.b_ids = Button(self, height=150, width=150, image=self.img_ids, command=callback)
        self.b_ids.grid(row=1, column=1, padx=(70, 50), pady=10)

        self.b_sox = Button(self, height=150, width=150, image=self.img_sox, command=callback)
        self.b_sox.grid(row=1, column=2, pady=10)

        self.b_sps = Button(self, height=150, width=150, image=self.img_sps, command=callback)
        self.b_sps.grid(row=2, column=1, padx=(70, 50), pady=5)

        self.b_dev = Button(self, height=150, width=150, image=self.img_dev, command=callback)
        self.b_dev.grid(row=2, column=2, pady=5)



if __name__ == '__main__':
    root = Tk()
    app = Window(root)
    root.grid()
    root.geometry("500x350")
    root.mainloop()