3 Various problems with ttk treeviews in python

I am doing a chat client using treeview with multiple columns in Python.

This is the code for 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 these elements:

 chat.insert("", "end", "", values=((user, message, time)), tags=(messageid)) chat.tag_configure(messageid, foreground='#ff0000') 

Now this works fine (here is a screenshot as an example): screenshot , but the last line of code changes the color of all three columns in this line. I want to change only the text color of column # 2 (message only), and not the entire row (not Nick or Time columns). I have been trying for a long time, but it's 4 in the morning and I give up. Is there any way to do this?

Update after 2 weeks

Now I tried to make 3 different trees (1 column each), and this ends as follows: Although this fixes the color problem, I have a new problem: scroll bar. Is this a way to bind a scrollbar to three different tree structures? all my attempts have been failed so far, and I can only move one of the tree images using the scroll bar. Is it possible to tie 3 trees? (If yes: how ?, worth ?, should I?)

And one more problem: all attempts to remove the treeview border failed in the TTK icon.

Another problem is that now only the first word is displayed in the Mensaje tree. I don’t know why neither one nor the other: \ this is the new code about the problem with the first word.

 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 post:

 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 +".") 
+6
source share
2 answers

1. First question: scroll bar

The solution consists of creating a top-level ttk.Treeview object and another tree for each column. Scroll activations are tied to the top level tree structure. This is a little more cumbersome than having three columns in the same tree, 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 ) 

Second question: border / crest

Use a style setting 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: this is an error (or function: p).

Windows completely ignores the -borderwidth . (more information on the comp.lang.tcl mailing comp.lang.tcl : http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2007-11/msg00923.html )

Third Question: truncature

This is the easiest question: the -values parameter expects iterable apply to each column. Example:

 for (col, value) in zip( tree.columns(), values ) : col.insert(value) 

Where the error is: a string also iterable ! (this is literally a char list), 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, etc., to ensure that the message should be applied as a whole, just add to the end: (idmensajeactual,)

This code works:

 chat2.insert("", "end", iid=(idmensajeactual,) , values=mensaje, tags=(messageid)) 

Finally

I loaded my stub as a github gist. You can check it out here and customize it to your needs: https://gist.github.com/lucasg/7643411

Output:

enter image description here

+11
source

I can answer part of your question: how to get rid of the Treeview border:

 style = ttk.Style() style.layout("Treeview", [ ('Treeview.treearea', {'sticky': 'nswe'}) ]) 
+6
source

Source: https://habr.com/ru/post/957273/


All Articles