尝试在画布中居中放置图像时出现错误

时间:2020-06-03 13:28:11

标签: python python-3.x tkinter tkinter-canvas

我正在尝试将图像居中放置在画布中,但是出现此错误。 IndexError: tuple index out of range 所有帮助我们赞赏:) 这是我的代码:

from tkinter import*

main = Tk()
main.config(bg="grey66")
main.minsize(650,450)
main.maxsize(650,450)

mainFrameHigh = Frame(main)
mainFrameHigh.grid(row=0,column=0)

mainFrameHighP = Frame(mainFrameHigh)
mainFrameHighP.grid(row=0,column=0)

canvasHigh = Canvas(mainFrameHighP,width=225,height=225)
canvasHigh.grid(row=0,column=0)
highway = PhotoImage(file = "/home/olijrhodes/tkinter/LSPD/LSPD_Photos/highway.png")
canvasHigh.create_image(anchor=CENTER,image=highway)

main.mainloop()

1 个答案:

答案 0 :(得分:2)

问题是您需要传递位置参数:

canvasHigh.create_image(anchor=CENTER,image=highway)

另请参阅document

anchor =

相对于给定位置放置图像的位置。默认值为CENTER。

所以也许您需要:

canvasHigh.create_image(225/2, 225/2, image=highway) # width/2 and height/2