Tkinter:将图像添加到窗口(使用画布上的create_window)

时间:2019-04-16 02:19:01

标签: python tkinter tkinter-canvas

我想在窗口的右上方添加一个图像。这是我要放在此处的图像:https://imgur.com/a/YjRWmMh

我尝试过这个:

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)

但是没有用。我遇到了这个错误。

文件“”,第22行

照片= PhotoImage(file =“ C:\ Users \ sel \ Desktop \ logo.png”)                            ^

SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置2-3中的字节:截断的\ UXXXXXXXX转义

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()

2 个答案:

答案 0 :(得分:1)

我认为是因为您为create_window函数提供了图像,而不是包含图像的标签。尝试改用create_image

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

答案 1 :(得分:1)

create_window用于将tkinter的小部件放在画布上。

PhotoImage不是窗口小部件。但是Label(内部带有PhotoImage)是一个小部件。

因此使用window=label代替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)

但是您宁愿使用create_image,就像@figbeam的答案一样。