"a simple vertically-scrollable canvas component and demo"
from tkinter import *
class ScrolledCanvas(Frame): def init(self, parent=None, color='brown'): Frame.init(self, parent) self.pack(expand=YES, fill=BOTH) # make me expandable canv = Canvas(self, bg=color, relief=SUNKEN) canv.config(width=300, height=200) # display area size canv.config(scrollregion=(0, 0, 300, 1000)) # canvas size corners canv.config(highlightthickness=0) # no pixels to border
sbar = Scrollbar(self)
sbar.config(command=canv.yview) # xlink sbar and canv
canv.config(yscrollcommand=sbar.set) # move one moves other
sbar.pack(side=RIGHT, fill=Y) # pack first=clip last
canv.pack(side=LEFT, expand=YES, fill=BOTH) # canv clipped first
self.fillContent(canv)
canv.bind('<Double-1>', self.onDoubleClick) # set event handler
self.canvas = canv
def fillContent(self, canv): # override me below
for i in range(10):
canv.create_text(150, 50+(i*100), text='spam'+str(i), fill='beige')
def onDoubleClick(self, event): # override me below
print(event.x, event.y)
print(self.canvas.canvasx(event.x), self.canvas.canvasy(event.y))
if name == 'main': ScrolledCanvas().mainloop()
image.png
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有