我的目标是创建一个画布,它作为一个bg图像,里面有一个框架,里面包含各种标签(标签的数量可以增长),为了模拟它,我创建了一个更大尺寸的框架而不是画布,但你可以看到,当你使用滚动条时,bg图像也会移动。 如何在不移动背景图像的情况下移动画布内的什么?甚至可能吗?
def pr_onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))
from tkinter import *
_F_CassaCenter = Tk()
#Master Frame to hold the canvas and scrollbar
_F_CarrelloMain = LabelFrame(_F_CassaCenter, text = "Window")
_F_CarrelloMain.pack()
#Canvas
_C_Maincanvas = Canvas(_F_CarrelloMain, width = 450, height = 300)
_C_Maincanvas.pack(side="left")
_C_Maincanvas.pack_propagate(False)
#Canvas Background image
bgimage = PhotoImage(file="C:\\Users\\Harry\\Desktop\\bg.png")
_C_Maincanvas.create_image(225,150,image = bgimage)
_C_Maincanvas.image_names()
#ScrollBar
_S_Carrello = Scrollbar(_F_CarrelloMain, orient="vertical", command=_C_Maincanvas.yview)
_S_Carrello.pack(side="left", fill="y")
_C_Maincanvas.configure(yscrollcommand=_S_Carrello.set)
#Inside the canvas
_F_OuterCarrelloInner = Frame(_C_Maincanvas, bg="orange")
_F_OuterCarrelloInner.pack()
_F_CarrelloInner = Frame(_F_OuterCarrelloInner, width = 100, height = 350,bg = "green")
_F_CarrelloInner.pack(padx=10, pady=10)
_F_CarrelloInner.pack_propagate(False)
#Canvas internal window to hold stuff that will grow
_C_Maincanvas.create_window((225, 150), window = _F_OuterCarrelloInner)
_F_OuterCarrelloInner.bind("<Configure>", lambda event, canvas=_C_Maincanvas: pr_onFrameConfigure(_C_Maincanvas))
_F_CassaCenter.mainloop()
修改
我将此功能附加到滚动条,但它将内框移动到画布右侧
def custom_yview(*args, **kwargs):
_C_Maincanvas.yview(*args, **kwargs)
x = _C_Maincanvas.canvasx(225)
y = _C_Maincanvas.canvasy(150)
_C_Maincanvas.coords("background", x,y)