0

So, I am trying to build an MP3-player. I have got my project structured like this:

  • source
    • main.py
  • content
    • controls.py
    • gui.py

Here is the code from those modules:

main.py

from tkinter import *
from source.content import gui

def main():
    # Defining our root window
    root = Tk()
    root.title("MMP3 - Mini MP3 Player")
    root.geometry("480x640")
    root.resizable(False, False)
    root.config(bg="#2B2B2B")

    gui.Gui(root)

    root.mainloop()

main()

gui.py

from source.content import controls

class Gui:
    def __init__(self, master):
        controls.Controls(master)

controls.py

from tkinter import *

class Controls:
    def __init__(self, master):
        self.controlsFrame = Frame(master)
        self.controlsFrame.grid(row=0, column=0)
        self.play_controls()

    def play_controls(self):
        # Getting button images
        self.img_prev = PhotoImage(file="E:/PyCharm Projects/Images/icons/previous.png")
        self.img_stop = PhotoImage(file="E:/PyCharm Projects/Images/icons/stop.png")
        self.img_play = PhotoImage(file="E:/PyCharm Projects/Images/icons/play.png")
        self.img_pause = PhotoImage(file="E:/PyCharm Projects/Images/icons/pause.png")
        self.img_next = PhotoImage(file="E:/PyCharm Projects/Images/icons/next.png")

        # Placing buttons
        self.btn_prev = Button(self.controlsFrame, image=self.img_prev, activebackground="#2B2B2B",
                               borderwidth=1)
        self.btn_prev.grid(row=0, column=0, ipadx="5px")
        self.btn_stop = Button(self.controlsFrame, image=self.img_stop, activebackground="#2B2B2B",
                               borderwidth=1)
        self.btn_stop.grid(row=0, column=1, ipadx="5px")
        self.btn_play = Button(self.controlsFrame, image=self.img_play, activebackground="#2B2B2B",
                               borderwidth=1)
        self.btn_play.grid(row=0, column=2, ipadx="5px")
        self.btn_pause = Button(self.controlsFrame, image=self.img_pause, activebackground="#2B2B2B",
                               borderwidth=1)
        self.btn_pause.grid(row=0, column=3, ipadx="5px")
        self.btn_next = Button(self.controlsFrame, image=self.img_next, activebackground="#2B2B2B",
                               borderwidth=1)
        self.btn_next.grid(row=0, column=4, ipadx="5px")

The problem is, when I try to create and place my buttons onto the screen, they take up space, but they are not clickable and contain no image. However, if I add text to them instead of images, everything works completely fine.

Screenshot

Also, if I run the code for images, button creation and placing from controls.py just in main, (as if there was no Controls class) then everything is good as well.

tokechu
  • 150
  • 1
  • 14

1 Answers1

0

I suspect that you are losing the reference to the images. In order to keep them you need to store the controls class as a variable inside your GUI class. Try this;

class Gui:
    def __init__(self, master):
        self.controls = controls.Controls(master)
scotty3785
  • 6,763
  • 1
  • 25
  • 35