this is what i got
And only when I maximize the window I can see the scroll bar
I want to stick the scrollbar (horizontal and vertical) in treeview. regardless of the window size.
I'm trying to add scrollbar to label with treeview regardless of the window size. this is my code:
def mainGUI():
root = tk.Tk()
root.geometry("700x300")
root.title("test")
root.columnconfigure(0, weight=1)
data = [["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"]]
results_lf = ttk.LabelFrame(root, text="Results:")
results_lf.grid(row=1, column=0, padx=20, pady=0, sticky='ew')
resultsLabel = Label(results_lf)
resultsLabel.pack(fill='x',expand=True, side=LEFT)
columnsHeader = ["1", "2", "3", "4", "5", "6"]
tree = ttk.Treeview(resultsLabel, columns=columnsHeader, show='headings')
tree.heading('1', text='1')
tree.heading('2', text='2')
tree.heading('3', text='3')
tree.heading('4', text='4')
tree.heading('5', text='5')
tree.heading('6', text='6')
for line in data:
tree.insert('', tk.END, values=line)
tree.pack(side=LEFT)
sb_v = Scrollbar(resultsLabel, orient=VERTICAL)
sb_v.pack(side=RIGHT, fill=Y)
sb_h = Scrollbar(resultsLabel, orient=HORIZONTAL)
sb_h.pack(side=BOTTOM, fill=X)
tree.config(yscrollcommand=sb_v.set)
sb_v.config(command=tree.yview)
sb_h.config(command=tree.xview)
root.mainloop()
mainGUI()
CodePudding user response:
When using pack
, the order in which you add widgets matters. pack
will allocate an entire side of the available space for each widget that is added.
When you pack the tree on the left, the packer allocates the entire left side for that widget. That means that things added to the top, right, or bottom will all be to the right of what was packed to the left. So, when you later pack the horizontal scrollbar it will be to the right of everything that is on the left.
The simple solution is to pack the two scrollbars before packing the tree. Personally I find it best to group together all calls to pack
or place
for children with a common parent. It makes visualizing the layout easier, and makes it easier to make changes like this.
So, if you move all of the calls to pack
for these three widgets to the end of the function, it should look like this:
sb_v.pack(side=RIGHT, fill=Y)
sb_h.pack(side=BOTTOM, fill=X)
tree.pack(side=LEFT)
For an answer with images that describe how the packer works, see