试图弄清楚如何在tkinter的树景,但最后一点撞到了墙上。我在将数据从最后一列移动到第一列时遇到了一些问题。代码注释中的进一步说明。希望这里的任何人都能为我指明正确的方向。
'''
from tkinter import ttk, Button, Entry, Label, Tk, Scrollbar, Frame
root = Tk()
root.geometry("800x600")
my_frame = Frame(root)
my_frame.pack()
# some value's so interperter doesn't freak out
a = 10
b = 30
c = 50
# list representing stuff from database query return
list_data = [(a, b, c, 1), (a, b, c, 2), (a, b, c, 3), (a, b, c, 4),
(a, b, c, 5), (a, b, c, 6), (a, b, c, 7), (a, b, c, 8), (a, b, c, 9),
(a, b, c, 10), (a, b, c, 11), (a, b, c, 12), (a, c, b, 13),
(a, b, c, 14),]
# list to hold column titles
cols = ["col1", "col2", "col3", "col4"]
tree = ttk.Treeview(my_frame, selectmode="browse")
tree.pack(side="left")
tree["columns"] = cols
tree["show"] = "headings"
for col in range(len(cols)):
tree.column(col, width=100, anchor="c")
tree.heading(col, text=cols[col])
#----------------------------------------------------------------------
# want to shift column 4's data representing rowid to column 1
# expected it just to be values=list_data[data-1] but that only shifts
# the data vertical inside column 4
#----------------------------------------------------------------------
for data in range(len(list_data)):
tree.insert("", "end", text=list_data[data], values=list_data[data])
side_scroller = Scrollbar(my_frame, orient="vertical", command=tree.yview)
side_scroller.pack(side="right", fill="y")
tree.configure(yscrollcommand=side_scroller.set)
root.mainloop()
'''
我已经花了很长时间才发现一些有用的东西,虽然不漂亮,但如果你知道更好的方法,可以随时发布改进。也许这将为某人省去一些挫折感作为起点。
'''
from tkinter import ttk, Button, Entry, Label, Tk, Scrollbar, Frame
root = Tk()
root.geometry("500x300")
my_frame = Frame(root)
my_frame.pack()
# some value's so interperter doesn't freak out
a = 10
b = 30
c = 50
# list representing stuff from database query return
list_data = [(a, b, c, 1), (a, b, c, 2), (a, b, c, 3), (a, b, c, 4),
(a, b, c, 5), (a, b, c, 6), (a, b, c, 7), (a, b, c, 8), (a, b, c, 9),
(a, b, c, 10), (a, b, c, 11), (a, b, c, 12), (a, c, b, 13),
(a, b, c, 14),]
# list to hold column titles
# changed col1 to row id and up to 3 for columns to make it more clear
cols = ["row id", "col1", "col2", "col3"]
tree = ttk.Treeview(my_frame, selectmode="browse")
tree.pack(side="left")
tree["columns"] = cols
tree["show"] = "headings"
for col in range(len(cols)):
tree.column(col, width=100, anchor="c")
tree.heading(col, text=cols[col])
# spaghetti code but it does work as intended
# think the problems came from the list being tuples
# even after converting to list of lists couldn't shift normally
#
# created some lists as placeholders
list1 = []
list_of_lists = []
# converted list off tuples to list of lists
list_of_lists = [list(elem) for elem in list_data]
# looping thru the list to change the order to get last entry first
# did try to do it in a shifting function but couldn't get it to work
for part in list_of_lists:
list1.append(part[-1])
list1.append(part[0])
list1.append(part[1])
list1.append(part[2])
# chopped it into pieces still hardcoded length of 4 in for readability of example
chunk = [list1[x:x+4] for x in range(0, len(list1), 4)]
# back to the original lines but calling chunk instead of list_data
for data in range(len(chunk)):
tree.insert("", "end", text=chunk[data], values=chunk[data])
side_scroller = Scrollbar(my_frame, orient="vertical", command=tree.yview)
side_scroller.pack(side="right", fill="y")
tree.configure(yscrollcommand=side_scroller.set)
root.mainloop()
,,,