标准的创建可滚动画布的例子

标准的创建可滚动画布的例子

"""
   标准的创建可滚动画布的例子
"""

from tkinter import *

class Test(Frame):
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        Pack.config(self)
        self.createWidgets()

    def createWidgets(self):
        self.question = Label(self, text="tkinter可滚动画布")
        self.question.pack()

        self.QUIT = Button(self, text='QUIT', background='red',
                           height=3, command=self.quit)
        self.QUIT.pack(side=BOTTOM, fill=BOTH)
        spacer = Frame(self, height="0.25i")
        spacer.pack(side=BOTTOM)

        # 创建画布,可滚动区域为20i,20i
        self.draw = Canvas(self, width="5i", height="5i",
                           background="dodger blue",
                           scrollregion=(0, 0, "20i", "20i"))
        # 创建滚动条
        self.draw.scrollX = Scrollbar(self, orient=HORIZONTAL)
        self.draw.scrollY = Scrollbar(self, orient=VERTICAL)

        # 在画布上设置好滚动条
        self.draw['xscrollcommand'] = self.draw.scrollX.set
        self.draw['yscrollcommand'] = self.draw.scrollY.set
        self.draw.scrollX['command'] = self.draw.xview
        self.draw.scrollY['command'] = self.draw.yview

        # 在画布上面画一个青色的矩形
        self.draw.create_rectangle(100, 100, "3.5i", "3.5i", fill="cyan")
       
        # 最终把滚动条放置好
        self.draw.scrollX.pack(side=BOTTOM, fill=X)
        self.draw.scrollY.pack(side=RIGHT, fill=Y)
        self.draw.pack(side=LEFT)


    def scrollCanvasX(self, *args):
        print("scrolling", args)
        print( self.draw.scrollX.get())



test = Test()

test.mainloop()

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。