我創建了一個TreeView,但我希望能夠在調用特定函數時隱藏某些列。所以這個TreeView最初被創建,但是當我調用一個函數時,可以說除[trackArtist]之外的所有列都是隱藏的。我該怎麼做呢?我可以通過在函數中重寫部分TreeView(我想要顯示的列)來讓它工作,但看起來非常多餘。在Tkinter中隱藏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)
請包括滾動環境,這是可以驗證/測試 – R4PH43L
這個開源項目可能會幫助你很多:https://github.com/talcs/tals_python_task_diary – SomethingSomething