"""
标准的创建可滚动画布的例子
"""
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()
-
- 2026 年 3 月
- 2026 年 2 月
- 2026 年 1 月
- 2025 年 12 月
- 2025 年 11 月
- 2025 年 10 月
- 2025 年 9 月
- 2025 年 6 月
- 2025 年 5 月
- 2025 年 3 月
- 2025 年 2 月
- 2025 年 1 月
- 2024 年 12 月
- 2024 年 8 月
- 2024 年 6 月
- 2024 年 5 月
- 2024 年 4 月
- 2024 年 3 月
- 2024 年 2 月
- 2023 年 11 月
- 2023 年 9 月
- 2023 年 6 月
- 2023 年 5 月
- 2023 年 4 月
- 2023 年 3 月
- 2023 年 2 月
- 2023 年 1 月
- 2022 年 12 月
- 2022 年 11 月
- 2022 年 10 月
- 2022 年 9 月
- 2022 年 8 月
- 2022 年 7 月
- 2022 年 6 月
- 2022 年 5 月
- 2022 年 4 月
- 2022 年 3 月
- 2022 年 2 月
- 2022 年 1 月
- 2021 年 12 月
- 2021 年 11 月
- 2021 年 10 月
- 2021 年 9 月
- 2021 年 8 月
- 2021 年 7 月
- 2021 年 6 月
- 2021 年 5 月
- 2021 年 4 月
- 2021 年 3 月
- 2021 年 2 月
- 2021 年 1 月
- 2020 年 12 月
- 2020 年 11 月
- 2020 年 10 月
- 2020 年 9 月
- 2020 年 8 月
- 2020 年 7 月
- 2020 年 6 月
- 2020 年 5 月
- 2020 年 4 月
- 2020 年 3 月
- 2020 年 2 月
- 2020 年 1 月
- 2019 年 12 月
- 2019 年 11 月
- 2019 年 10 月
- 2019 年 9 月
- 2019 年 8 月
- 2019 年 7 月
- 2019 年 6 月
- 2019 年 5 月
- 2019 年 4 月
- 2019 年 3 月
- 2019 年 2 月
- 2018 年 3 月
- 2018 年 1 月
- 2017 年 9 月
- 2017 年 5 月
- 2017 年 1 月
