python scoll canvas

2022-05-13 10:44:54 浏览数 (1)

"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

代码语言:javascript复制
    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

0 人点赞