-3

i have a window and i am trying to add an image to it. the window so far consists of a label entry box and button. i want to add the image below the label but above the entry box button. Here is my code:

    master = Tk()
    master.wm_title("Lightning Parties")
    master.configure(background='lightgreen')
    Label(master, text="Staff Login", fg='black', bg='lightgreen', font=    ('comicsans', 14)).grid()
    Label(master, text="Please enter the password ", fg='black', bg='lightgreen', font=('comicsans', 12)).grid(row=7)

    Password= Entry(master)

    Password.grid(row=7, column=1)

    Button(master, text='Login', command=validateStaff, fg='black', bg='white', font=('comicsans', 12)).grid(row=10, column=1, sticky=W, pady=4)

The image i want to add has the file name 'sonic.png'.

Harshdeep Sokhey
  • 324
  • 5
  • 15
brenda
  • 73
  • 1
  • 2
  • 9
  • 3
    What problem are you having that can't be solved by reading available documentation? There are examples all over the internet showing how to add images to tkinter programs. – Bryan Oakley Jan 27 '17 at 17:04
  • 3
    As @BryanOakley correctly stated, you should do some debugging at your own end once before posting on SO. Having said that have a look at these posts. 1. [tkinter-photoimage-doesnt-not-support-png-image](http://stackoverflow.com/questions/27599311/tkinter-photoimage-doesnt-not-support-png-image) 2. [how-to-add-an-image-in-tkinter-python-2-7](http://stackoverflow.com/questions/10133856/how-to-add-an-image-in-tkinter-python-2-7) – Harshdeep Sokhey Jan 27 '17 at 17:07
  • 1
    read tutorial or documentation: ie. [The Tkinter PhotoImage Class](http://effbot.org/tkinterbook/photoimage.htm) – furas Jan 27 '17 at 18:02
  • 1
    @brenda Its not rude. Its just how SO works. Before you approach another person with a problem, you should do some digging at your end. Asking people for solutions without debugging is rather #rude! #NoOffense – Harshdeep Sokhey Jan 27 '17 at 18:08
  • i did debug it, and looked through documentation and i can't get my head around it. just because i don't dedicate my whole life to programming doesn't mean i should be disrespected and made to look stupid. we all struggle with different things, this is just my issue. SO is a place people go for help as a last resort. do you not think i have already spent hours trying to fix and research this myself? because for your information i did. if you have nothing nice to say don't say anything at all! #peaceout – brenda Jan 27 '17 at 20:37

1 Answers1

-1

You can create a PhotoImage with the image you want, this will work for .png, .gif and lots of other image files.

Simply put your image in a Label and put the Label in the grid

from tkinter import *

master = Tk()
master.wm_title("Lightning Parties")
master.configure(background='lightgreen')
Label(master, text="Staff Login", fg='black', bg='lightgreen', font=    ('comicsans', 14)).grid()
Label(master, text="Please enter the password ", fg='black', bg='lightgreen', font=('comicsans', 12)).grid(row=7)

Password= Entry(master)    
Password.grid(row=7, column=1)

my_image = PhotoImage(master = master, file = "Your_image.png") # your image
label = Label(master, image = my_image) # put the image on a label
label.grid(row = 6, column = 0, columnspan = 2) # put the label in the grid

Button(master, text='Login', command=validateStaff, fg='black', bg='white', font=('comicsans', 12)).grid(row=10, column=1, sticky=W, pady=4)

Also @Brenda the people commenting on your post are just trying to help you :) When I first joined SO some of my posts got down voted, you should learn from mistakes rather than being defensive :)

Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
  • thanks, i tried this code but when i tried to run an error came up and said 'image "pyimage1" doesn't exist'. my image is called 'sonic' so i just changed the 'your_image.png' part of the code to 'sonic.gif'. any ideas where it has gone wrong? thanks again – brenda Jan 28 '17 at 12:35
  • is your image in the same folder as your Program? If not, that may be the cause of the error – Tom Fuller Jan 29 '17 at 13:10
  • I think the error saying that pyimage1 doesn't exist is because the image isn't linked to a master. Hopefully if you add `master = master` to the `PhotoImage` it should work – Tom Fuller Feb 07 '17 at 18:55