首页 > 解决方案 > tkinter 按钮在窗口中不起作用或显示图像

问题描述

所以我对用 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()

给出这个输出:

[错误的输出[1]

顶部菜单工作正常,但按钮不执行任何操作,并且按钮上的图像不显示。

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

如果相反,代码是:

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

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

运作良好

并且点击按钮也会做他们应该做的事情(在这种情况下只需打印“点击”)。我的理解有限,但这并不理想,我想让我的按钮在窗口类中初始化,而不是在“主方法”中。有人可以帮我弄清楚为什么会这样吗?

标签: pythonooptkintertkinter-canvas

解决方案


创建按钮时,需要保留图像,否则它们会被垃圾收集器销毁。

我没有你的图片,但我的它可以工作

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

推荐阅读