How to use an image for the background in tkinter?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







13















#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image


Code to import image:



app = Tk()
app.title("Welcome")
image2 =Image.open('C:\Users\adminp\Desktop\titlepage\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\Usfront.png')
#app.configure(background = image1)

labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')

label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()

app.mainloop()


This code doesn't work. :( I want to import a background image.










share|improve this question

























  • Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

    – gary
    Apr 15 '12 at 1:14








  • 3





    I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

    – joaquin
    Apr 15 '12 at 7:39











  • Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

    – gary
    Apr 15 '12 at 17:50


















13















#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image


Code to import image:



app = Tk()
app.title("Welcome")
image2 =Image.open('C:\Users\adminp\Desktop\titlepage\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\Usfront.png')
#app.configure(background = image1)

labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')

label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()

app.mainloop()


This code doesn't work. :( I want to import a background image.










share|improve this question

























  • Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

    – gary
    Apr 15 '12 at 1:14








  • 3





    I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

    – joaquin
    Apr 15 '12 at 7:39











  • Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

    – gary
    Apr 15 '12 at 17:50














13












13








13


7






#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image


Code to import image:



app = Tk()
app.title("Welcome")
image2 =Image.open('C:\Users\adminp\Desktop\titlepage\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\Usfront.png')
#app.configure(background = image1)

labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')

label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()

app.mainloop()


This code doesn't work. :( I want to import a background image.










share|improve this question
















#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image


Code to import image:



app = Tk()
app.title("Welcome")
image2 =Image.open('C:\Users\adminp\Desktop\titlepage\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\Usfront.png')
#app.configure(background = image1)

labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')

label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()

app.mainloop()


This code doesn't work. :( I want to import a background image.







python tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 '17 at 23:18









martineau

70.5k1093186




70.5k1093186










asked Apr 15 '12 at 0:24









user1276381user1276381

1091311




1091311













  • Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

    – gary
    Apr 15 '12 at 1:14








  • 3





    I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

    – joaquin
    Apr 15 '12 at 7:39











  • Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

    – gary
    Apr 15 '12 at 17:50



















  • Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

    – gary
    Apr 15 '12 at 1:14








  • 3





    I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

    – joaquin
    Apr 15 '12 at 7:39











  • Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

    – gary
    Apr 15 '12 at 17:50

















Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

– gary
Apr 15 '12 at 1:14







Does your label show the "Wecome !!!!" text if you remove the image attribute? i.e. Label(app, textvariable=labelText, ...) This text would not show up of the image was showing up, since the image attribute takes precedence over textvariable.

– gary
Apr 15 '12 at 1:14






3




3





I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

– joaquin
Apr 15 '12 at 7:39





I will send a proposal to SO for deprecation of the term "not working". It lost any useful meaning the very first day it was born. Seriously, try to describe why you think it is not working.

– joaquin
Apr 15 '12 at 7:39













Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

– gary
Apr 15 '12 at 17:50





Deleted my answer since it wasn't answering your actual question. You should edit your post to make it more specific. I'd also use the link you posted in your comment to formulate an answer for this question. (It's perfectly fine to answer your own questions here.)

– gary
Apr 15 '12 at 17:50












2 Answers
2






active

oldest

votes


















20














One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.



For example:



background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.



Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:



background_label.image = background_image





share|improve this answer





















  • 1





    Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

    – town_math
    Feb 27 '14 at 19:50













  • How do I add background image with a text overlay?

    – the_prole
    Nov 16 '14 at 19:21



















3














A simple tkinter code for Python 3 for setting background image .



from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop





share|improve this answer
























  • It Worked.Will You Please Give Me code to make two buttons on front of it.

    – user8167727
    Jul 1 '17 at 9:40












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10158552%2fhow-to-use-an-image-for-the-background-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









20














One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.



For example:



background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.



Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:



background_label.image = background_image





share|improve this answer





















  • 1





    Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

    – town_math
    Feb 27 '14 at 19:50













  • How do I add background image with a text overlay?

    – the_prole
    Nov 16 '14 at 19:21
















20














One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.



For example:



background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.



Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:



background_label.image = background_image





share|improve this answer





















  • 1





    Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

    – town_math
    Feb 27 '14 at 19:50













  • How do I add background image with a text overlay?

    – the_prole
    Nov 16 '14 at 19:21














20












20








20







One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.



For example:



background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.



Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:



background_label.image = background_image





share|improve this answer















One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.



For example:



background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.



Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:



background_label.image = background_image






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 23 '17 at 23:20

























answered Apr 16 '12 at 20:45









Bryan OakleyBryan Oakley

222k22277438




222k22277438








  • 1





    Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

    – town_math
    Feb 27 '14 at 19:50













  • How do I add background image with a text overlay?

    – the_prole
    Nov 16 '14 at 19:21














  • 1





    Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

    – town_math
    Feb 27 '14 at 19:50













  • How do I add background image with a text overlay?

    – the_prole
    Nov 16 '14 at 19:21








1




1





Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

– town_math
Feb 27 '14 at 19:50







Thanks for this, Bryan. To get it to work on my program I had to add the line background_label.photo=background before the last line.

– town_math
Feb 27 '14 at 19:50















How do I add background image with a text overlay?

– the_prole
Nov 16 '14 at 19:21





How do I add background image with a text overlay?

– the_prole
Nov 16 '14 at 19:21













3














A simple tkinter code for Python 3 for setting background image .



from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop





share|improve this answer
























  • It Worked.Will You Please Give Me code to make two buttons on front of it.

    – user8167727
    Jul 1 '17 at 9:40
















3














A simple tkinter code for Python 3 for setting background image .



from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop





share|improve this answer
























  • It Worked.Will You Please Give Me code to make two buttons on front of it.

    – user8167727
    Jul 1 '17 at 9:40














3












3








3







A simple tkinter code for Python 3 for setting background image .



from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop





share|improve this answer













A simple tkinter code for Python 3 for setting background image .



from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 '16 at 6:49









Abhijeet SinhaAbhijeet Sinha

316




316













  • It Worked.Will You Please Give Me code to make two buttons on front of it.

    – user8167727
    Jul 1 '17 at 9:40



















  • It Worked.Will You Please Give Me code to make two buttons on front of it.

    – user8167727
    Jul 1 '17 at 9:40

















It Worked.Will You Please Give Me code to make two buttons on front of it.

– user8167727
Jul 1 '17 at 9:40





It Worked.Will You Please Give Me code to make two buttons on front of it.

– user8167727
Jul 1 '17 at 9:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10158552%2fhow-to-use-an-image-for-the-background-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?