Im making an app in which i want to set an image background to my Canvas. Is it possible? If yes how? I've tried a lot of thing but work. Here is the code
root=Tk()
bg=PhotoImage(file="bgbg.jpg")
canvas1 = Canvas(root, image=bg,width = 400, height = 800)
canvas1.pack()```
CodePudding user response:
Try this code.
from tkinter import *
root=Tk()
bg=PhotoImage(file="bgbg.png")
canvas1 = Canvas(root,width = 400, height = 800)
canvas1.create_image((200,400),image=bg) # here (200,400) is x and y.
canvas1.pack()
root.mainloop()
CodePudding user response:
#Import the required library
from tkinter import *
YELLOW = "#f7f5dd"
#Create root
root= Tk()
root.title("My Title")
#define geometry
root.geometry("400x800")
#Load the image
background= PhotoImage(file="example.png")
#Create a canvas
canvas= Canvas(root,width= 400, height= 800)
canvas.pack(expand=True, fill= BOTH)
#Add the image in the canvas -> "nw" = start "North West"
canvas.create_image(0,0,image=background, anchor="nw")
#Add a text in canvas
canvas.create_text(200,400,text="It Works", font= ('Courier 45 bold'), fill=YELLOW)
root.mainloop()