Reputation: 1975
How can I prevent the widgets in this app from changing size when I press the toggle headings button?
I would like the two Treeviews to always take up 50% of the width of the window each
Currently the left one resizes when it gets more/less columns
import tkinter as tk
from tkinter import ttk
def toggle_headings():
global htog
if htog==0:
t1.config(columns=columnswide)
for i,h in enumerate(columnswide):
c = ('#')+str(i)
t1.heading(c,text=h)
htog = 1
else:
t1.config(columns=columns)
t1.heading(columns[0],text=columns[0])
htog = 0
htog = 0
app = tk.Tk()
columnswide = ['abcd' for i in range(5)]
columns = ['abcd']
columnsbutton = tk.Button(app,text='toggle headings',command=toggle_headings)
t1 = ttk.Treeview(app,selectmode='browse',columns=columns)
t2 = ttk.Treeview(app,selectmode='browse',columns=columnswide)
for i,h in enumerate(columnswide):
c = ('#')+str(i)
t2.heading(c,text=h)
t1.heading(columns[0],text=columns[0])
for i in range(10):
t1.insert('','end',text='testing '+str(i),values=['value'])
t2.insert('','end',text='testing '+str(i),values=['value' for i in range(5)])
columnsbutton.pack(side='top',fill='x',expand=1)
t1.pack(side='left',fill='both',expand=1)
t2.pack(side='left',fill='both',expand=1)
app.mainloop()
In case anyone suggests using .grid(), I have tried this method below:
import tkinter as tk
from tkinter import ttk
def toggle_headings():
global htog
if htog==0:
t1.config(columns=columnswide)
for i,h in enumerate(columnswide):
c = ('#')+str(i)
t1.heading(c,text=h)
htog = 1
else:
t1.config(columns=columns)
t1.heading(columns[0],text=columns[0])
htog = 0
htog = 0
app = tk.Tk()
columnswide = ['abcd' for i in range(5)]
columns = ['abcd']
columnsbutton = tk.Button(app,text='toggle headings',command=toggle_headings)
t1 = ttk.Treeview(app,selectmode='browse',columns=columns)
t2 = ttk.Treeview(app,selectmode='browse',columns=columnswide)
for i,h in enumerate(columnswide):
c = ('#')+str(i)
t2.heading(c,text=h)
t1.heading(columns[0],text=columns[0])
for i in range(10):
t1.insert('','end',text='testing '+str(i),values=['value'])
t2.insert('','end',text='testing '+str(i),values=['value' for i in range(5)])
columnsbutton.grid(row=0,column=0,columnspan=2,sticky='we')
t1.grid(row=1,column=0,sticky='nswe')
t2.grid(row=1,column=1,sticky='nswe')
app.grid_columnconfigure(0,weight=1)
app.grid_columnconfigure(1,weight=1)
app.mainloop()
Upvotes: 2
Views: 349
Reputation: 2952
To explain @new_to_coding's solution in more detail:
Use grid_columnconfigure()
for Tkinter
.
As the first parameter of grid_columnconfigure()
you need to set the column index.
Note that there are additional parameters. For example, some column can grow more than other when resized.
minsize
: Defines the minimum size for the column. Note that if a column is completely empty, it will not be displayed, even if this option is set.pad
: Padding to add to the size of the largest widget in the column when setting the size of the whole column.weight
: A relative weight used to distribute additional space between columns. A column with the weight 2 will grow twice as fast as a column with weight 1. The default is 0, which means that the column will not grow at all.uniform
: When a non-empty value is supplied, places the column in a uniform group
with other columns that have the same value for uniform
. The space for columns belonging to a uniform group is allocated so that their sizes are always in strict proportion to their weight
values.Upvotes: 1
Reputation: 1975
I have solved the problem with this option:
app.grid_columnconfigure(0,weight=1,uniform='x')
app.grid_columnconfigure(1,weight=1,uniform='x')
it makes each column proportional size
Upvotes: 1