tkinter按钮放在窗口中时不起作用或显示图像

时间:2018-08-31 19:58:26

标签: python oop tkinter tkinter-canvas

因此,我刚开始使用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()

给出以下输出:

[Wrong output[1]

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

虽然我将按钮的代码移到main方法中(如果if name ==' main ':部分,这在python中是正确的名称吗? ?),它开始起作用。

相反,代码是:

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

一切开始正常工作,就像这样:

Working well

单击按钮也可以实现预期的效果(在这种情况下,只需打印“单击”)。我的理解是有限的,但这不是理想的,我想在window类而不是“ main方法”中初始化我的按钮。有人可以帮我弄清楚为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

创建按钮时,您需要保留图像,否则图像将被垃圾收集器破坏。

我没有您的图片,但是可以用我的图片

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