使用Tkinter中的Scrollbar对象创建滑动条,同时使用Canvas对象来绘制图形,请给出示例
时间: 2024-04-29 17:25:01 浏览: 120
Sure! Here's an example code:
```python
from tkinter import *
# Create a window
root = Tk()
# Create a canvas with a scrollbar
canvas = Canvas(root)
scrollbar = Scrollbar(root, orient=VERTICAL, command=canvas.yview)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.pack(side=LEFT, fill=BOTH, expand=True)
canvas.configure(yscrollcommand=scrollbar.set)
# Draw some shapes on the canvas
canvas.create_rectangle(50, 50, 100, 100, fill="red")
canvas.create_oval(150, 50, 200, 100, fill="blue")
canvas.create_polygon(250, 50, 300, 100, 250, 100, fill="green")
# Run the main loop
root.mainloop()
```
This code creates a window with a canvas and a vertical scrollbar. It also draws three shapes (a rectangle, an oval, and a triangle) on the canvas. You can modify the canvas and shapes to suit your needs. Hope this helps!
阅读全文