python如和在place放置下的Treeview中增加水平滚动轴
时间: 2024-02-27 17:51:42 浏览: 27
要在 Python 中在已经放置 Treeview 的 place 中增加水平滚动轴,你可以使用 `Scrollbar` 和 `xscrollcommand` 参数实现。以下是一个简单的示例代码:
```python
from tkinter import *
root = Tk()
# 创建一个带有水平滚动轴的 Treeview
tree = ttk.Treeview(root, columns=('col1', 'col2', 'col3'), show='headings')
tree.column('col1', width=100, anchor='center')
tree.column('col2', width=100, anchor='center')
tree.column('col3', width=100, anchor='center')
tree.heading('col1', text='列1')
tree.heading('col2', text='列2')
tree.heading('col3', text='列3')
# 添加数据
for i in range(100):
tree.insert('', 'end', values=(i, i+1, i+2))
# 放置 Treeview
tree.place(x=50, y=50, width=320, height=200)
# 创建水平滚动轴
xscrollbar = Scrollbar(root, orient='horizontal', command=tree.xview)
xscrollbar.place(x=50, y=250, width=320)
# 将水平滚动轴与 Treeview 绑定
tree.configure(xscrollcommand=xscrollbar.set)
root.mainloop()
```
在这个示例中,我们先创建了一个带有列名的 Treeview,并添加了一些数据,然后将它放置在 `(50, 50)` 的位置,并设置了宽度和高度。接着,我们创建了一个水平滚动轴 `xscrollbar`,并将其放置在 Treeview 下方的 `(50, 250)` 的位置,并设置了宽度。最后,我们使用 `configure` 方法将 Treeview 的 `xscrollcommand` 参数设置为 `xscrollbar.set`,以将两者绑定在一起。
这样,当 Treeview 中的数据宽度超过 Treeview 的宽度时,就会出现水平滚动轴,以便用户可以滚动查看所有的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)