Last active
May 3, 2021 13:14
-
-
Save lucasg/7643411 to your computer and use it in GitHub Desktop.
ttk Treeview object for stackoverflow question : http://stackoverflow.com/questions/19749476/3-different-issues-with-ttk-treeviews-in-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ttk | |
import Tkinter as tk | |
def insert_message(columns, (user,message,time), messageid): | |
for col, var in zip(columns, (user,message,time) ): | |
col.insert("", "end", "", values=(var,), tags=(messageid)) | |
def tag_configure( messageid, column, **kwargs ): | |
for col in columns: | |
col.tag_configure("", "end", "", values=(var,), tags=(messageid)) | |
def create_columns( master = None ): | |
chat1 = ttk.Treeview( master = bot, height="28", columns="Mensaje", selectmode="extended") | |
chat1.heading('#1', text='Nick', anchor=tk.CENTER) | |
chat1.column('#1', stretch=tk.YES, minwidth=50, width=130) | |
chat1.column('#0', stretch=tk.NO, minwidth=0, width=0) | |
chat2 = ttk.Treeview( master = bot, height="28", columns="Mensaje", selectmode="extended") | |
chat2.heading('#1', text='Mensaje', anchor=tk.CENTER) | |
chat2.column('#1', stretch=tk.YES, minwidth=400, width=620) | |
chat2.column('#0', stretch=tk.NO, minwidth=0, width=0) | |
chat3 = ttk.Treeview( master = bot, height="28", columns="Mensaje", selectmode="extended") | |
chat3.heading('#1', text='Hora', anchor=tk.CENTER) | |
chat3.column('#1', stretch=tk.YES, minwidth=25, width=65) | |
chat3.column('#0', stretch=tk.NO, minwidth=0, width=0) | |
return (chat1, chat2, chat3) | |
# Tkinter master Frame | |
root = tk.Tk() | |
# start of Notebook (multiple tabs) | |
notebook = ttk.Notebook(root) | |
notebook.pack(fill=tk.BOTH, expand=True) | |
notebook.pressed_index = None | |
# Top level Treeview object | |
bot = ttk.Treeview(notebook) | |
bot2 = ttk.Treeview(notebook) | |
# 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 ) | |
user = 'Captain Obvious' | |
message = 'This is a message, please ignore.' | |
time= 'Now !' | |
messageid = "kkk" | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
insert_message(columns, (user,message,time), messageid) | |
columns[1].tag_configure( messageid, foreground = "#ff0000" ) | |
ttk.Style().configure( '.', | |
relief = 'flat', | |
borderwidth = 0, | |
) | |
################################################## | |
## Packing | |
vsb.pack(side = tk.RIGHT , fill = tk.Y) | |
hsb.pack(side = tk.BOTTOM , fill = tk.X) | |
map( lambda col : col.pack(side = tk.LEFT, fill = tk.BOTH, expand = True) , columns) | |
bot.pack(fill = tk.BOTH, expand = True) | |
bot2.pack(fill = tk.BOTH, expand = True) | |
notebook.add(bot, text='Mode A') | |
notebook.add(bot2, text='Mode B') | |
### Launching | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment