简体   繁体   中英

Tkinter : Adding image to the window (create_window on the canvas is used)

I want to add an image in right top of the window. This is the image I want to put up there : https://imgur.com/a/YjRWmMh

I tried this :

photo = PhotoImage(file="C:\Users\sel\Desktop\logo.png")

tk.label = Label(canvas, image=photo)

canvas.create_window(125,10, window=photo, anchor=tk.NW)

But it didn t work.I got this error.

File "", line 22

photo = PhotoImage(file="C:\\Users\\sel\\Desktop\\logo.png") ^

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape

import tkinter as tk


window = tk.Tk()
window.configure()

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 800  # width for the Tk root
h = 600  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)

window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="white", width=800, height=600)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=())


#photo = PhotoImage(file="C:\Users\sel\Desktop\logo.png")
#tk.label = Label(canvas, image=photo)
#canvas.create_window(125,10, window=photo, anchor=tk.NW)


l0 = tk.Label(canvas, text="강재 NESTING 최적화 프로그램", font= "calibri 32",fg="royalblue4",bg="white" )
canvas.create_window(125,70, window=l0, anchor=tk.NW)


l1 = tk.Label(canvas, text="MIN")
canvas.create_window(125,150, window=l1, anchor=tk.NW)


l2 = tk.Label(canvas, text="MAX")
canvas.create_window(260,150, window=l2, anchor=tk.NW)


l3 = tk.Label(canvas, text="Sheet 폭(SW)")
canvas.create_window(3,170, window=l3, anchor=tk.NW)

minw_var = tk.DoubleVar()
entry_minw_number = tk.Entry(canvas, textvariable=minw_var)
canvas.create_window(190,180, window=entry_minw_number)

maxw_var = tk.DoubleVar()
entry_maxw_number = tk.Entry(canvas, textvariable=maxw_var)
canvas.create_window(325,180, window=entry_maxw_number)

l4 = tk.Label(canvas, text="Sheet 높이(SH)")
canvas.create_window(3,190, window=l4, anchor=tk.NW)

minl_var = tk.DoubleVar()
entry_minl_number = tk.Entry(canvas, textvariable=minl_var)
canvas.create_window(190,200, window=entry_minl_number)

maxl_var = tk.DoubleVar()
entry_maxl_number = tk.Entry(canvas, textvariable=maxl_var)
canvas.create_window(325,200, window=entry_maxl_number)

l5 = tk.Label(canvas, text="소요 Component 개수")
canvas.create_window(3,210, window=l5, anchor=tk.NW)

rect_var = tk.IntVar()
entry_rect_number = tk.Entry(canvas, textvariable=rect_var)
canvas.create_window(260,220, window=entry_rect_number)

l6 = tk.Label(canvas, text="Area")
canvas.create_window(3,230, window=l6, anchor=tk.NW)

area_var = tk.DoubleVar()
entry_area_number = tk.Entry(canvas, textvariable=area_var)
canvas.create_window(260,240, window=entry_area_number)


window.title("Rectangle Configuration")
window.mainloop()

I think it's because you supply the create_window function with the image instead of the label containing the image. Try using create_image instead:

photo = tk.PhotoImage(file=r"C:\Users\sel\Desktop\logo.png")
canvas.create_image(125,10, image=photo, anchor=tk.NW)

create_window is used to put tkinter's widget on canvas.

PhotoImage is not widget. But Label (with PhotoImage inside) is a widget.

So use window=label instead of window=photo

photo = tk.PhotoImage(file="C:\Users\sel\Desktop\logo.png")
label = tk.Label(canvas, image=photo)
canvas.create_window(125,10, window=label, anchor=tk.NW)

But you would rather use create_image as in @figbeam's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM