![]() elif col='yellow': nfigure('Treeview',background="yellow", fieldbackground="yellow", foreground="black", font=font1)Ĭonfiguring Tkinter Treeview style background foreground color using Radio buttonsįull code is here from tkinter import ttkimport tkinter as tk# Creating tkinter my_wmy_w = tk.Tk()my_w.geometry("300x250") # width and height of the window my_w.title("trv=ttk.Treeview(my_w,selectmode='browse',show='headings',height=5)trv.grid(row=1,column=1,columnspan=3,padx=30,pady=10)# column identifiers trv = ("1", "2")# Defining headings, other option is treetrv = 'headings tree' # width of columns and alignment trv.column("#0", width = 80, anchor ='c')trv.column("1", width = 10, anchor ='c')trv.column("2", width = 100, anchor ='c')# Headings # respective columnstrv.heading("#0", text ="Label",anchor='c')trv.heading("1", text ="id")trv.heading("2", text ="Name",anchor='c')trv.insert("",'end',iid=1,text='First',values=(1,'n1-Alex'))trv.insert("",'end',iid=2,text='second',values=(2,'n2-Ravi'))trv.insert("",'end',iid=3,text='third',values=(3,'n3-Ronn'))style = ttk.Style(my_w) style.theme_use("clam") # set theam to nfigure("Treeview", background="black", fieldbackground="black", foreground="white")nfigure('Treeview.Heading', background="PowderBlue")r1_v = tk.StringVar(value='black') # We used string variable heredef my_upd(col): if col='white': nfigure('Treeview',background="white", fieldbackground="white", foreground="black") elif col='yellow': nfigure('Treeview',background="yellow", fieldbackground="yellow", foreground="black") else: nfigure('Treeview',background="black", fieldbackground="black", foreground="white")r1 = tk.Radiobutton(my_w, text='Black', variable=r1_v, value='black', command=lambda:my_upd('black'))r1.grid(row=2,column=1) r2 = tk.Radiobutton(my_w, text='White', variable=r1_v, value='white', command=lambda:my_upd('white'))r2.grid(row=2,column=2) r3 = tk.Radiobutton(my_w, text='Yellow', variable=r1_v, value='yellow', command=lambda:my_upd('yellow'))r3.grid(row=2,column=3) my_w.How to use tkinter Treeview? The most typical is table and file directory traversal. ![]() font1=To one of the option we can assign the font style like this. ![]() def my_upd(col): if col='white': nfigure('Treeview',background="white", fieldbackground="white", foreground="black") elif col='yellow': nfigure('Treeview',background="yellow", fieldbackground="yellow", foreground="black") else: nfigure('Treeview',background="black", fieldbackground="black", foreground="white")We can include the font style also. ![]() r1 = tk.Radiobutton(my_w, text='Black', variable=r1_v, value='black', command=lambda:my_upd('black'))Inside the function my_upd() we will check the parameter value and accordingly update the style. On click of the radio button we will trigger the function my_upd(col) and we passed colour as parameter. We used on StringVar() with default value as 'black'. nfigure('Treeview', rowheight=100) Managing style using Radio buttonsUsing three RadioButtons we will configure the style of Treeview. style = ttk.Style(my_w) style.theme_use("clam") # set theam to nfigure("Treeview", background="black", fieldbackground="black", foreground="white")nfigure('Treeview.Heading', background="PowderBlue")The last line in above code adds different background color to headings. Assigning default style for TreeviewThis is applied to all Treeview widgets.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |