我已经创建了一个TreeView,但是当我调用特定的函数时,我希望能够隐藏某些列。所以这个TreeView最初是创建的,但是当我调用一个函数时,可以说除了[trackArtist]之外所有的列都被隐藏了。我该怎么做呢?我可以通过在函数中重写TreeView的部分(我想要出现的列)来使它工作,但这看起来非常多余。
def some_function(self):
self.tree = ttk.Treeview(self.stationBuilderMenuMiddleFrame)
ttk.Style().configure("displayField", background="#850664", foreground="#000000", fieldbackground="#FFFFFF")
ysb = ttk.Scrollbar(self.stationBuilderMenu, orient=tk.VERTICAL, command=self.tree.yview)
xsb = ttk.Scrollbar(self.stationBuilderMenu, orient=tk.HORIZONTAL, command=self.tree.xview)
self.tree.configure(yscrollcommand=ysb.set)
self.tree.configure(xscrollcommand=xsb.set)
self.tree["columns"]=("trackNumber", "trackTitle", "artistCat", "artistDisplay", "trackArtist", "album",
"albumLabel", "composer", "year", "trackDuration", "albumAsin", "albumItunes", "ISRC",
"filename", "albumCoverName", "notes")
self.tree.column("trackNumber", stretch=0, anchor=N)
self.tree.column("trackTitle", stretch=0, anchor=N)
self.tree.column("artistCat", stretch=0, anchor=N)
self.tree.column("artistDisplay", stretch=0, anchor=N)
self.tree.column("trackArtist", stretch=0, anchor=N)
self.tree.column("album", stretch=0, anchor=N)
self.tree.column("albumLabel", stretch=0, anchor=N)
self.tree.column("composer", stretch=0, anchor=N)
self.tree.column("year", stretch=0, anchor=N)
self.tree.column("trackDuration", stretch=0, anchor=N)
self.tree.column("albumAsin", stretch=0, anchor=N)
self.tree.column("albumItunes", stretch=0, anchor=N)
self.tree.column("filename", stretch=0, anchor=N)
self.tree.column("albumCoverName", stretch=0, anchor=N)
self.tree.column("notes", stretch=0, anchor=N)
self.tree.heading("trackNumber", text="Track Number")
self.tree.heading("trackTitle", text="Track Title")
self.tree.heading("artistCat", text="Artist CAT")
self.tree.heading("artistDisplay", text="Artist Display")
self.tree.heading("trackArtist", text="Track Artist")
self.tree.heading("album", text="Album")
self.tree.heading("albumLabel", text="Album Label")
self.tree.heading("composer", text="Composer")
self.tree.heading("year", text="Year")
self.tree.heading("trackDuration", text="Track Duration")
self.tree.heading("albumAsin", text="Album ASIN")
self.tree.heading("albumItunes", text="Album iTunes")
self.tree.heading("ISRC", text="ISRC")
self.tree.heading("filename", text="File Name")
self.tree.heading("albumCoverName", text="Album Cover Filename")
self.tree.heading("notes", text="Notes")
self.tree.grid(in_=self.stationBuilderMenuMiddleFrame, row=0, column=0, sticky=tk.NSEW)
ysb.grid(in_=self.stationBuilderMenuMiddleFrame, row=0, column=1, sticky=tk.NS)
xsb.grid(in_=self.stationBuilderMenuMiddleFrame, row=1, column=0, sticky=tk.EW)
self.stationBuilderMenuMiddleFrame.rowconfigure(0, weight=1)
self.stationBuilderMenuMiddleFrame.columnconfigure(0, weight=1)
这是我扔在一起的黑客代码。不能很好地工作。
def some_other_function(self):
# things happen
# inefficient code below
self.tree["columns"]=("artistCat", "artistDisplay")
self.tree.column("artistCat", stretch=0, anchor=N)
self.tree.column("artistDisplay", stretch=0, anchor=N)
self.tree.heading("artistCat", text="Artist CAT")
self.tree.heading("artistDisplay", text="Artist Display")
class StationBuilder():
def __init__(self, master, importTree):
self.importTree = importTree
self.stationBuilderMenu = master
self.stationBuilderMenu = Toplevel(master)
self.stationBuilderMenuTopFrame = Frame(self.stationBuilderMenu, bg="#BFC0C2", bd=1, relief=RIDGE)
self.stationBuilderMenuMiddleFrame = Frame(self.stationBuilderMenu)
self.stationBuilderMenuBottomFrame = Frame(self.stationBuilderMenu, bg="#BFC0C2", bd=1, relief=RIDGE)
self.stationBuilderMenuRightFrame = Frame(self.stationBuilderMenu, bg="#BFC0C2", bd=1, relief=RIDGE, width=300)
self.stationBuilderMenu.wm_title("AccuAdmin - toStationBuilder")
self.stationBuilderMenu.configure(background="#BFC0C2")
self.currentTrack = 0
self.currentState = 0
self.artistObjectId = ""
self.albumObjectId = ""
self.composerObjectId = ""
self.trackObjectId = ""
self.treeId = ""
self.states = [self.search_artist, self.search_album, self.search_composer, self.search_track]
self.match = Button(self.stationBuilderMenuBottomFrame, text="Match", highlightbackground="#BFC0C2",
command=self.exists)
self.createNewObject = Button(self.stationBuilderMenuBottomFrame, text="", highlightbackground="#BFC0C2",
command=self.create_new_object)
self.currentObject = Label(self.stationBuilderMenuTopFrame, text="", font=("default", 50), bg="#BFC0C2",
fg="#FFFFFF")
self.currentlyProcessedTrackHeading = Label(self.stationBuilderMenuRightFrame, text="- Current Track -",
bg="#BFC0C2")
self.currentlyProcessedTrack = Label(self.stationBuilderMenuRightFrame, text="", bg="#BFC0C2")
self.create_potential_match_display()
self.stationBuilderMenuBottomFrame.pack(side=BOTTOM, fill=X)
self.stationBuilderMenuTopFrame.pack(side=TOP, fill=X)
self.stationBuilderMenuRightFrame.pack(side=RIGHT, fill=Y)
self.stationBuilderMenuMiddleFrame.pack(side=LEFT, fill=BOTH)
self.currentObject.pack(side=LEFT, padx=(25, 0))
self.currentlyProcessedTrackHeading.pack(side=TOP)
self.currentlyProcessedTrack.pack(side=TOP)
self.match.pack(side=RIGHT, padx=(0, 25))
self.createNewObject.pack(side=LEFT, padx=(25, 0))
self.states[self.currentState](importedTracks[self.currentTrack])
self.tree.bind("<<TreeviewSelect>>", self.find_selection)
答案 0 :(得分:5)
您应该可以使用
这样做 self.tree["displaycolumns"]=("artistCat", "artistDisplay")
在Documentation上找到。
要显示除一个以外的所有内容,您可以在for循环中构建它
exclusionlist=[...]
displaycolumns=[]
for col in tree["columns"]:
if not "%s"%col in exclusionlist:
displaycolumns.append(col)
tree["displaycolumns"]=displaycolumns
查找以下不会弄乱数据的工作代码。 仅使用tree["displaycolumns"]=[...]
#!/usr/bin/env python2
#
# FILE: SO_33290969.py
# CREATED: 2015-10-28
__CHANGED__="2015-11-02 17:54:55"
__VERSION__=1.1
import Tkinter as tk
import ttk
from Tkinter import *
class some_Class(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.master=args[0]
self.grid()
self.some_function()
def some_function(self):
self.tree = ttk.Treeview(self.master)
ttk.Style().configure("displayField", background="#850664", foreground="#000000", fieldbackground="#FFFFFF")
ysb = ttk.Scrollbar(self.master, command=self.tree.yview, orient=tk.VERTICAL)
xsb = ttk.Scrollbar(self.master, command=self.tree.xview, orient=tk.HORIZONTAL)
self.tree.configure(yscrollcommand=ysb.set)
self.tree.configure(xscrollcommand=xsb.set)
self.tree["columns"]=("trackNumber", "trackTitle", "artistCat", "artistDisplay", "trackArtist", "album",
"albumLabel", "composer", "year", "trackDuration", "albumAsin", "albumItunes", "ISRC",
"filename", "albumCoverName", "notes")
self.tree.column("trackNumber", stretch=0, anchor=N)
self.tree.column("trackTitle", stretch=0, anchor=N)
self.tree.column("artistCat", stretch=0, anchor=N)
self.tree.column("artistDisplay", stretch=0, anchor=N)
self.tree.column("trackArtist", stretch=0, anchor=N)
self.tree.column("album", stretch=0, anchor=N)
self.tree.column("albumLabel", stretch=0, anchor=N)
self.tree.column("composer", stretch=0, anchor=N)
self.tree.column("year", stretch=0, anchor=N)
self.tree.column("trackDuration", stretch=0, anchor=N)
self.tree.column("albumAsin", stretch=0, anchor=N)
self.tree.column("albumItunes", stretch=0, anchor=N)
self.tree.column("filename", stretch=0, anchor=N)
self.tree.column("albumCoverName", stretch=0, anchor=N)
self.tree.column("notes", stretch=0, anchor=N)
self.tree.heading("trackNumber", text="Track Number")
self.tree.heading("trackTitle", text="Track Title")
self.tree.heading("artistCat", text="Artist CAT")
self.tree.heading("artistDisplay", text="Artist Display")
self.tree.heading("trackArtist", text="Track Artist")
self.tree.heading("album", text="Album")
self.tree.heading("albumLabel", text="Album Label")
self.tree.heading("composer", text="Composer")
self.tree.heading("year", text="Year")
self.tree.heading("trackDuration", text="Track Duration")
self.tree.heading("albumAsin", text="Album ASIN")
self.tree.heading("albumItunes", text="Album iTunes")
self.tree.heading("ISRC", text="ISRC")
self.tree.heading("filename", text="File Name")
self.tree.heading("albumCoverName", text="Album Cover Filename")
self.tree.heading("notes", text="Notes")
self.tree.grid(in_=self.master, row=0, column=0, sticky=tk.NW+tk.SE)
ysb.grid(in_=self.master, row=0, column=1, sticky=tk.NS)
xsb.grid(in_=self.master, row=1, column=0, sticky=tk.EW)
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.loadUI()
def loadUI(self, event=None):
tk.Button(self.master, text="filter columns", command=self.some_other_function).grid(row=2, column=0)
tk.Button(self.master, text="load data", command=self.loadData).grid(row=2, column=1)
def loadData(self, event=None):
self.tree.insert("", tk.END, values=(["data%s"%col for col in self.tree["columns"]]))
def some_other_function(self):
if len(self.tree["displaycolumns"]) != 2:
print("Hiding Columns")
self.tree["displaycolumns"]=("artistCat", "artistDisplay")
else:
print("Showing all columns")
self.tree["displaycolumns"]=self.tree["columns"]
print("Visible: %s"%','.join([ "%s"%col for col in self.tree["displaycolumns"] ]))
print("Available: %s"%','.join([ "%s"%col for col in self.tree["columns"] ]))
print("Done.")
if __name__=="__main__":
root=tk.Tk()
app=some_Class(root)
app.grid()
root.mainloop()
答案 1 :(得分:1)
tree = ttk.Treeview(srchFrame, columns=("ID #","Abbreviation","Name"), height=5)
tree.heading('#1', text='ID #', anchor=tk.CENTER)
tree.heading('#2', text='Abbrev', anchor=tk.CENTER)
tree.heading('#3', text='Name', anchor=tk.CENTER)
#set width to zero for column zero in order to hide it
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=30, anchor=tk.CENTER)
tree.column('#2', stretch=NO, minwidth=0, width=60, anchor=tk.CENTER)
tree.column('#3', stretch=NO, minwidth=0, width=160, anchor=tk.W)
tree.insert("", "end", "", values=("1","US","United States"))
tree.grid(row=1, column=3)
答案 2 :(得分:-1)
tree.heading("1", text="")
tree.heading("2", text="Name",command = lambda : treeview_sort_column(tree, "2", False))
tree.heading("3", text="POli Number",command = lambda : treeview_sort_column(tree, "3", False))
tree.heading("4", text="Number Of People",command = lambda: treeview_sort_column(tree, "4", False))
tree.heading("5", text="Ticket Number",command = lambda : treeview_sort_column(tree, "5", False))
tree.heading("6", text="Final amount",command = lambda : treeview_sort_column(tree, "6", False))
tree.heading("7", text="Username",command = lambda : treeview_sort_column(tree, "7", False))
tree["displaycolumns"]=("2", "3", "4", "5", "6", "7")