I am doing a chat client using a treeview
with multiple columns in Python.
This is the code for the treeview
:
chat = ttk.Treeview(height="26", columns=("Nick","Mensaje","Hora"), selectmode="extended")
chat.heading('#1', text='Nick', anchor=W)
chat.heading('#2', text='Mensaje', anchor=W)
chat.heading('#3', text='Hora', anchor=W)
chat.column('#1', stretch=NO, minwidth=0, width=130)
chat.column('#2', stretch=NO, minwidth=0, width=620)
chat.column('#3', stretch=NO, minwidth=0, width=65)
chat.column('#0', stretch=NO, minwidth=0, width=0) #width 0 to not display it
And I add items like this:
chat.insert("", "end", "", values=((user, message, time)), tags=(messageid))
chat.tag_configure(messageid, foreground='#ff0000')
Now, that works perfectly (here's a screenshot as an example): but that last line of code changes the colour of all 3 columns in that row. What I want is to change only the colour of the text of the #2 column (just the message) and not the entire row (not Nick or Time columns). I tried for a long time now but it's 4 AM and I surrender ☹ Is there any way to do it?
Updating 2 weeks later
Now I tried to do 3 different treeviews (1 column each) and it ends up this way: Although that fix the colour issue, i have a new issue: The scrollbar. It's there a way to bound a scrollbar to 3 different treeviews? all my attemps had failed so far and i can move only one of the treeview with a scrollbar. It's possible to bound to 3 treeviews? (If yes: how?, worth?, should i?)
And also another problem: all attempts to remove treeview border have failed in TTK python.
Another problem is that now the Mensaje treeview only displays the first word. No idea why neither :\ this is the new code about the first word issue.
chat2 = ttk.Treeview(height="28", columns="Mensaje", selectmode="extended")
chat2.heading('#1', text='Mensaje', anchor=CENTER)
chat2.column('#1', stretch=NO, minwidth=400, width=620)
chat2.column('#0', stretch=NO, minwidth=0, width=0)
And this goes on message:
BotGUI.chat2.insert("", "end", iid=(idmensajeactual), values=mensaje, tags=(messageid))
try:
BotGUI.chat2.tag_configure(messageid, foreground='#'+colorfuente) #tfl
except TclError:
print("[Error02] - can't assign colour of "+ usuario +".")
Python Tkinter Treeview is a tabular representation of the data as it has all the properties of the table. Treeview has rows, columns, and heading. rows: Horizontal space determined by the data. More data more rows. columns: vertical space determines the heading.
ttk is a module that is used to style the tkinter widgets. Just like CSS is used to style an HTML element, we use tkinter. ttk to style tkinter widgets.
The Treeview widget items can be edited and deleted by selecting the item using tree. selection() function. Once an item is selected, we can perform certain operations to delete or edit the item.
A solution consist of creating a top-level ttk.Treeview object, and another tree for every column. The scrollbars activations are linked to the top-level tree view. It's a bit more cumbersome than having the three columns in the same tree object, but it works :
# Top level Treeview object
bot = ttk.Treeview( Tkinter.Tk() )
# Columns (treeview objects also)
columns = create_columns( bot)
################################
## Scrollbars
vsb = ttk.Scrollbar( bot,
orient="vertical",
command = bot.yview
)
hsb = ttk.Scrollbar( bot,
orient="horizontal",
command = bot.xview
)
## Link scrollbars activation to top-level object
bot.configure( yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
## Link scrollbar also to every columns
map ( lambda col : col.configure( yscrollcommand=vsb.set,xscrollcommand=hsb.set), columns )
Use the style configuration object
ttk.Style().configure( '.', # every class of object
relief = 'flat', # flat ridge for separator
borderwidth = 0, # zero width for the border
)
However it will not work on Windows : it is a bug (or a feature :p ).
Windows completely ignores the -borderwidth
parameter. (more infos on the comp.lang.tcl
mailing list : http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2007-11/msg00923.html )
That's the easiest question : the parameter -values
expect an iterable
for to apply to each columns. Ex:
for (col, value) in zip( tree.columns(), values ) :
col.insert(value)
That's where the bug is : a string
is also an iterable
! (it is literally a list of char) so when you try to call insert
with the message "This is a message"
, ttk will apply "This"
to the first column, "is"
to the second, and so on ...
To enforce the fact that the message should be applied as a whole, just add a coma at the end : (idmensajeactual,)
This code works :
chat2.insert("", "end", iid=(idmensajeactual,) , values=mensaje, tags=(messageid))
I've uploaded my stub as a github gist. You can check it out here and tweak it to your needs : https://gist.github.com/lucasg/7643411
Output :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With