简体   繁体   中英

tkinter buttons not functioning or showing image when placed in window

So I'm pretty new to writing object oriented code in Python and also very new to making GUIs. I need help understanding why the following does not show any image on the buttons and why the buttons don't work but the top menu works fine:

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

Gives this output:

[ 错误的输出[1]

The top menu is working fine but the buttons do not do anything and the images on the buttons don't show up.

While if I move the code for the buttons into the main method (would this be the correct name in python for the if name == ' main ': portion ?), it starts to work.

If instead the code is:

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

Everything starts to work fine like so:

运作良好

And clicking the buttons also does what they're supposed to (in this case just print "click"). My understanding is limited but this is not ideal, I would like to have my buttons initialized in the window class and not in the "main method". Can someone help me figure out why this might be the case?

When you create buttons, you need to keep images, otherwise, they are destroyed by the garbage collector.

I don't have your images, but with my it works

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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