尝试将图像置于 canvas 中心时出现错误
I get an error when trying to centre my image in a canvas
我试图将我的图像置于 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()
问题是您需要传递位置参数:
canvasHigh.create_image(anchor=CENTER,image=highway)
另请参阅 document:
anchor=
Where to place the image relative to the given position. Default is CENTER.
所以也许您需要:
canvasHigh.create_image(225/2, 225/2, image=highway) # width/2 and height/2
我试图将我的图像置于 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()
问题是您需要传递位置参数:
canvasHigh.create_image(anchor=CENTER,image=highway)
另请参阅 document:
anchor=
Where to place the image relative to the given position. Default is CENTER.
所以也许您需要:
canvasHigh.create_image(225/2, 225/2, image=highway) # width/2 and height/2