简体   繁体   English

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

[英]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. 因此,我对于使用Python编写面向对象的代码非常陌生,对于制作GUI也是非常新的。 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. 当我将按钮的代码移到main方法中时(如果if name ==' main ':part?,这是python中正确的名称),它将开始工作。

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". 我的理解是有限的,但这不是理想的,我想在window类而不是“ main方法”中初始化我的按钮。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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