[英]Python 3 tkinter treeview, having trouble shifting data from last to first column
試圖弄清楚如何在 tkinter 的 treeview 中顯示數據庫中的一些數據,但在最后一點上碰壁了。 我在將數據從最后一列移動到第一列時遇到了一些麻煩。 代碼注釋中的進一步描述。 希望這里的任何人都可以為我指明正確的方向。
'''
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()
,,,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.