1

I am new to Tkinter. I want to create a GUI that can support embedding audio files and that also has a background image. I have tried endlessly to install pygame to no avail. I cannot seem to figure out why it is not installing correctly so at this point I am just trying to find the easiest way possible to have these two options. Below is my attempt at displaying a background image using a canvas widget. However, I always get an error that my variables are not defined. I would really appreciate some feedback on what I am doing wrong, as well as any helpful tkinter tutorials that involve more than just the basics. Thanks in advance

from Tkinter import *   

root = Tk()
root.geometry("500x500")

class Application(Frame):

    def __init__(self, master):
        #initialize the frame
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.can = Canvas(root, width=160, height=160, bg='white')
        self.pic = PhotoImage(file='speaker.gif')
        self.item = can.create_image(80, 80, image=pic)

app = Application(root)

#kick off event loop
root.mainloop()

1 Answer 1

1

Every time you want to use an attribute of a class inside one of its methods, you need to prefix it with self.:

self.item = self.can.create_image(80, 80, image=self.pic)
#           ^^^^^                               ^^^^^

Otherwise, Python will treat the names as being local to the function and will raise an exception when it fails to find them.

Also, you forgot to call grid on your canvas widget:

self.can = Canvas(root, width=160, height=160, bg='white')
self.can.grid(...)

As for resources on Tkinter, you can check out these:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.