Reputation: 21
Apologies if this was answered before but I cannot find anything out there with these specifics.
I'm finishing a query tool for the team I'm working with where they can choose the fields to query a database and a search a string (along with other options).
The result is being sent to a Tkinter TreeView widget (found this to be the best approach in terms of events control and visualization).
I have a main issue which is to constrain the size of the Treeview to a certain width, no matter how many fields the user chooses. Since the complete GUI is non scalable I want the Treeview to have a max size.
I have tried to include the width when defining the columns, but it seems to bypass that.
The Treeview is a child of a LabelFrame and the positioning is defined with grid.
Here a code sample of what I'm doing to set the TreeView (since this is a company application I have to be careful with disclosing some field names):
CoreQuery.groupResults = LabelFrame(CoreQuery.root, text="Query Result", padx=5, pady=5, height=470,width=960)
CoreQuery.tree = ttk.Treeview(CoreQuery.groupResults, selectmode='browse')
CoreQuery.tree.bind("<Double-1>", CoreQuery.OnTreeDoubleClick)
CoreQuery.scrollbar_horizontal = ttk.Scrollbar(CoreQuery.root, orient='horizontal', command=CoreQuery.tree.xview)
CoreQuery.scrollbar_vertical = ttk.Scrollbar(CoreQuery.root, orient='vertical', command=CoreQuery.tree.yview)
CoreQuery.tree.config(height=18, xscrollcommand=CoreQuery.scrollbar_horizontal.set, yscrollcommand=CoreQuery.scrollbar_vertical.set)
CoreQuery.tree.grid(row=0, sticky="w")
CoreQuery.scrollbar_horizontal.grid(row=1,column=0, sticky='ns')
CoreQuery.scrollbar_vertical.grid(row=0, column=2, sticky='ew')
CoreQuery.scrollbar_horizontal.configure(command=CoreQuery.tree.xview)
CoreQuery.scrollbar_vertical.configure(command=CoreQuery.tree.yview)
CoreQuery.tree.configure(yscroll=CoreQuery.scrollbar_vertical, xscroll=CoreQuery.scrollbar_horizontal)
The Following is the method that receives the SQL query result and places the data in the TreeView:
def ScreenPrintResults(header,rows):
columns=len(header)
minColumnsize=math.ceil(965/columns)
#Clear Treeview
CoreQuery.tree.delete(*CoreQuery.tree.get_children())
for values in rows:
values[0] = str(values[0]).replace(".0", "")
if (values[0].isdigit()):
values[0] = int(values[0])
auxCount=0
CoreQuery.tree['columns']=header
for value in header:
CoreQuery.tree.heading(value, text=str(value))
CoreQuery.tree.column(value, stretch=tk.NO)
for items in rows:
if auxCount==0:
CoreQuery.tree.column('#0', width=30, stretch=tk.NO)
else:
CoreQuery.tree.column(value, width=minColumnsize)
CoreQuery.tree.insert('',tk.END,auxCount+1,text=str(auxCount+1),
values=list(items))
auxCount=auxCount+1
CoreQuery.updateMessage.config(foreground="Black", font="Verdana 10 bold")
CoreQuery.message.set("...")
Is there any kind of limitation I can add the width of the TreeView so that it does not go beyond a certain width? Or do I need to split the available width to the number of columns I get from the query?
I honestly don't care if all the information is on screen, hence I placed the scrollbars.
Here is a screenshots of the issue:
Thanks for the help!
EDIT: Changed the For loop code and added more clarifying screenshots of the issue
Upvotes: 0
Views: 1581
Reputation: 1474
The example below demonstrate how to set width for the columns
the minwith
and maxwidth
which takes integers
. Also you can set height of the row using the rowheight
attribute for your text to display well in the tree.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root, column=("col1","col2"), show="headings")
style = ttk.Style(root)
style.configure('my.Treeview', rowheight=50)
tree.configure(style='my.Treeview')
tree.heading("#0", text="COMPANY NAME")
tree.heading("#1", text="DEPARTMENT")
tree.heading("#2", text="BRANCH")
tree.column("#0", stretch=tk.NO, minwidth=100, width=000)
tree.column("#1", stretch=tk.NO, minwidth=100, width=400)
tree.column("#2", stretch=tk.NO, minwidth=100, width=400)
tree.insert("", tk.END, values=("Am using python version 3.6.1 \n on windows machine ", "This an example Tkinter Treeview in Python, which is from \nttk class make"))
tree.insert("", tk.END, values=("This an example Tkinter Treeview in Python, which is from \nttk class make sure #import ttk\n also from tkinter import *", "Apologies if this was answered before but I cannot find "))
tree.pack()
root.mainloop()
Upvotes: 0