我有一个问题:当表格中有大量列时,它们会随着滚动超出窗口字段
请告诉我出了什么问题
我的代码:
from tkinter import *
from tkinter import ttk
class Main:
def __init__(self):
self.root = Tk()
self.root.geometry('500x300')
def run(self):
self.interface()
self.content()
def interface(self):
frame = Frame()
frame.pack(fill = BOTH, expand=1)
self.tree = ttk.Treeview(frame)
self.tree.pack(fill = BOTH, expand=1, side=LEFT)
sc_y = ttk.Scrollbar(frame, orient="vertical", command=self.tree.yview)
sc_y.pack(side=RIGHT, fill=Y)
self.tree["yscrollcommand"]=sc_y.set
def content(self):
people = [("Tom", 38, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Tom", 38, "[email protected]", "Tom", 38, "[email protected]")]
columns = ("name", "age", "email", "name1", "age1", "email1")
self.tree.config(columns=columns, show="headings")
self.tree.heading("name", text="Имя")
self.tree.heading("age", text="Возраст")
self.tree.heading("email", text="Email")
self.tree.heading("name1", text="Имя")
self.tree.heading("age1", text="Возраст")
self.tree.heading("email1", text="Email")
for i in columns:
self.tree.column(i, stretch=NO)
for person in people:
self.tree.insert("", END, values=person)
if __name__ == '__main__':
A = Main()
A.run()
我已经审阅了所有的手册、教程。没有人写过这个问题。
这是我所做的小修改。这是一个如何定义
Scrollbar
以及在哪里定义的问题。更改位于函数 interface()
中。
from tkinter import *
from tkinter import ttk
class Main:
def __init__(self):
self.root = Tk()
self.root.geometry('500x300')
def run(self):
self.interface()
self.content()
self.root.mainloop()
def interface(self):
frame = Frame()
frame.pack(fill = BOTH, expand=1)
sc_y = ttk.Scrollbar(frame, orient="vertical")
sc_y.pack(side=RIGHT, fill=Y)
self.tree = ttk.Treeview(frame, yscrollcommand=sc_y.set)
self.tree.pack(fill = BOTH, expand=1, side=LEFT)
sc_y.config(command=self.tree.yview)
def content(self):
people = [("Tom", 38, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
("Tom", 38, "[email protected]", "Tom", 38, "[email protected]")]
columns = ("name", "age", "email", "name1", "age1", "email1")
self.tree.config(columns=columns, show="headings")
self.tree.heading("name", text="Имя")
self.tree.heading("age", text="Возраст")
self.tree.heading("email", text="Email")
self.tree.heading("name1", text="Имя")
self.tree.heading("age1", text="Возраст")
self.tree.heading("email1", text="Email")
for i in columns:
self.tree.column(i, stretch=NO)
for person in people:
self.tree.insert("", END, values=person)
if __name__ == '__main__':
A = Main()
A.run()
这里我向您展示输出 GIF:
更改的是滚动条声明的顺序,以便程序执行流程在正确的位置显示滚动条。
# First place
sc_y = ttk.Scrollbar(frame, orient="vertical")
sc_y.pack(side=RIGHT, fill=Y)
# Second place
self.tree = ttk.Treeview(frame, yscrollcommand=sc_y.set)
self.tree.pack(fill = BOTH, expand=1, side=LEFT)
关键是按这个顺序
pack()
,如this答案中提到的: