Tkinter Treeview运行主窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tkinter Treeview运行主窗口相关的知识,希望对你有一定的参考价值。

我正在尝试使用可变数量的列创建一个Treeview,当列数使TreeView比主窗口宽时,允许水平滚动。现在我实现了垂直和水平滚动条。永远不会启用水平滚动条,因为包含它的TreeView和Frame都会展开以适应所有列,并且多余的列从主窗口运行。但是,无论行数如何,TreeView的高度都不会更改。如果有人可以帮助我模仿横向的行为,那将非常感激。

TreeView太大,无法看到Vertical Scrollbar或水平滚动条的右端

TreeView适合,两个滚动条都可以看到

我试图在下面包含相关代码:

class Application(Frame):

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.dora = DoraExplorer()

    self.currentDict = {}
    self.lbox_list = []
    self.prevIndex =''
    self.pack()
    self.create_widgets()

def populateTreeView(self,tabDict):
    header = tabDict['header']
    data = tabDict['data']
    self.tree["columns"] = header
    self.tree['show'] = 'headings'
    self.tree.delete(*self.tree.get_children())

    for head in header:
        self.tree.column(head, width=10, anchor='c')
        self.tree.heading(head, text=head)
    #print(data)
    for j in range(len(data)):
        row = list(data[j])
        self.tree.insert("",'end',text=str(j),values=row)

def queryAction(self):
    #self.frame.delete('1.0',END)
    d = self.dbVar.get()
    t = self.tableVar
    cf = self.query.get()
    queryResults = self.dora.runQueries(cf,d,t)

    queryText = queryResults['text']
    label = queryText['label']
    title = queryText['title']


    self.populateTreeView(queryResults['table'])
def create_widgets(self):

    self.query = StringVar()
    self.createDBMenu()
    self.search_var = StringVar()
    self.search_var.trace("w", self.update_list)
    self.scrollbar = Scrollbar(self, orient=VERTICAL)
    self.lbox = Listbox(self, width=45, height=15,yscrollcommand=self.scrollbar.set)
    self.lbox.bind('<<ListboxSelect>>',self.onselect)
    self.lbox.grid(row=4, column=1,rowspan=9, padx=10, pady=3)

    self.entry = Entry(self, textvariable=self.search_var, width=40)
    self.entry.grid(row=3, column=1, padx=10, pady=3)

    self.cRadio = Radiobutton(self,text='Count',variable=self.query,value ='countTop',indicatoron=0)
    self.cRadio.grid(row=13,column=1)

    self.fRadio = Radiobutton(self,text='Foreign',variable=self.query,value ='foreignKeys',indicatoron=0)
    self.fRadio.grid(row=14,column=1)

    self.queryButton = Button(self, text="Query", command=self.queryAction)
    self.queryButton.grid(row=9,column=2,padx=3,pady=.05)

    self.treeFrame = Frame(self,width = 300, height = 400)
    self.treeFrame.grid(row=4, column=3,columnspan=3,rowspan=9, padx=10, pady=3)
    self.treeFrame.columnconfigure(3,weight=1)

    self.tree = ttk.Treeview(self.treeFrame, selectmode='browse')
    self.tree.grid(row=1, column=0, sticky=NSEW,in_=self.treeFrame, columnspan=3, rowspan=9)
    self.tree.grid_propagate(False)
    self.scbHDirSel =Scrollbar(self.treeFrame, orient=HORIZONTAL, command=self.tree.xview)
    self.scbVDirSel =Scrollbar(self.treeFrame, orient=VERTICAL, command=self.tree.yview)
    self.scbVDirSel.grid(row=1, column=50, rowspan=50, sticky=NS, in_=self.treeFrame)
    self.scbHDirSel.grid(row=52, column=0, rowspan=2,columnspan=3, sticky=EW,in_=self.treeFrame)
    self.tree.configure(yscrollcommand=self.scbVDirSel.set, xscrollcommand=self.scbHDirSel.set) 

root = Tk()

root.title("Tk dropdown example")

app= Application(master=root)
app.mainloop()
答案

结束切换到wxPython,因为他们的Grid类本身就是这样做的

SimpleGrid

以上是关于Tkinter Treeview运行主窗口的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Tkinter 清除整个 Treeview

Tkinter,treeview不会调整大小

python的tkinter运行不了

tkinter中treeview隔行显示不同的颜色

Tkinter教程

python 3 - tkinter - ttk treeview:查看列文本