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 +".")
1. First Question : Scrollbar
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 :
Second Question : The border/ridge
Use the style configuration object
However it will not work on Windows : it is a bug (or a feature :p ).
Windows completely ignores the
-borderwidth
parameter. (more infos on thecomp.lang.tcl
mailing list : http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2007-11/msg00923.html )Third Question : the truncature
That's the easiest question : the parameter
-values
expect aniterable
for to apply to each columns. Ex:That's where the bug is : a
string
is also aniterable
! (it is literally a list of char) so when you try to callinsert
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 :
Finally
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 :
I can answer part of your question: how to get rid of Treeview border: