附加到treeview时如何使两个滚动条滚动

时间:2019-06-07 14:23:01

标签: tkinter treeview

我有几行代码,它们已将垂直和水平滚动条附加到treeview上,但是只有垂直滚动条滚动了,但是希望它们都执行各自的功能。

import tkinter as tk
import tkinter.ttk as ttk



var2 = [("fred","How to fit a child widget when it is "), ("john","Tkinter Help Importing as well"),("emmanuel","How to access objects in other classes"),("ben","Trying to insert PILLOW image from instance class [duplicate]"),("AGNES","tkinter all widgets ontop of eachother"),("ERICA","Is there a way to increase or decrease the thickness of a shape's outline on a tkinter canvas? If so, how do I do it? Here's some come that can be used as an example. How could the rectangle's "),("BERNICE","I was in a project to design a chat system that would reply client from a Tkinter window"),("GIFTY","I am trying to insert PILLOW image from instance class."),("VAN"," larger than its parent container?"), ("FOSTER","tkinter all widgets ontop of eachother")]


root = tk.Tk()
root.geometry("500x200")
root.resizable(width=tk.FALSE, height=tk.FALSE)

frame = tk.Frame(root)
frame.pack(expand=True, fill=tk.BOTH)

tree = ttk.Treeview(frame, column=("col1","col2"))
tree.heading("#1", text="NAME")
tree.heading("#2", text="INFORMATION")

tree.column("#0", stretch=tk.NO, minwidth=0, width=0)
tree.column("#2", stretch=tk.NO, minwidth=100, width=150)

scrollbar1 = ttk.Scrollbar(frame, orient="vertical")
scrollbar1.pack(fill=tk.Y, side=tk.RIGHT)
tree.configure(yscrollcommand=scrollbar1.set)
scrollbar1.configure(command=tree.yview)


scrollbar1 = ttk.Scrollbar(frame, orient="horizontal")
scrollbar1.pack(fill=tk.X, side=tk.BOTTOM)
tree.configure(xscrollcommand=scrollbar1.set)
scrollbar1.configure(command=tree.xview)


for row in var2:
    tree.insert("", tk.END, values=(row))

tree.pack(expand=True, fill=tk.BOTH)


root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的代码没有错。您的单杠工作正常。

如果您想更好地查看它,请增加第2列的宽度:

tree.column("#2", stretch=tk.NO, minwidth=100, width=450)