-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resizing columns automatically to fit the frame if they would be smaller in total than the available space #227
Comments
Hi there, Have you tried the option Or do you mean resizing them to fit the cell text rather than a width in pixels? |
In my testing just now
Is there a good/an easier way to do this? |
Sorry about the confusion with Basically the value is for the minimum width in pixels of each individual column, so you could set this to About your question, I'm afraid I can only point to the relevant functions at the moment, especially the one below There is an internal function in the ColumnHeaders class, accessible from the self.CH.set_col_width(
col=column int,
width=None, # set to text size
only_set_if_too_small=False,
displayed_only=True, # if you have a lot of rows
return_new_width=True or False, # when True this will not set the column width but return the width it was going to be set to
) Looking at the available I will have to add some more functions for dealing with row heights / column widths, they may not have the functionality you seek above but by using them maybe you'll get to what you want. I can't offer a timeframe on this work though sorry |
I see, thank you for your help anyways. def resize_columns_to_fit(self, n=5):
minimum_scale_factor = 15
scrollbar_width = 17
if (self._headers is None
or self._is_shown is False
or self._show_mode == NO_DATA):
return
# Calculate the weight for each column based on the largest of the first n cells
weights = []
for col_idx in range(len(self._headers)):
# Get the first n cells in the current column
col_data = [len(str(self._data[row_idx][col_idx])) for row_idx in range(min(n, len(self._data)))]
# Include the header in the calculation
col_data.append(len(str(self._headers[col_idx])))
# Calculate the weight based on the largest item
max_width = max(col_data)
weights.append(max_width)
# Get the width of the sheet widget
root_window.update_idletasks() # Ensure the window is updated before getting its width
winfo_width = self._sheet.winfo_width()
total_weight = sum(weights)
# Proportionally adjust the weights to fit the widget width
if total_weight == 0:
scale_factor = minimum_scale_factor
else:
scale_factor = max(winfo_width / total_weight, minimum_scale_factor)
weights = [int(weight * scale_factor) for weight in weights]
# Adjust the last column to ensure the total width matches the widget width
if scale_factor > minimum_scale_factor:
weights[-1] += winfo_width - sum(weights)
if len(self._headers) == 1:
self._sheet.set_all_column_widths(weights[-1] - scrollbar_width)
return
# Set each column width
for col_idx, weight in enumerate(weights):
self._sheet.column_width(col_idx, weight) |
### Version 7.2.2 #### Added: - Add functions to address [227](#227): - `get_column_text_width` - `get_row_text_height` - `visible_columns` - `@property` - `visible_rows` - `@property` #### Changed: - Internal parameter names: - `only_set_if_too_small` -> ` only_if_too_small` - Internal function parameters: - `set_col_width` - `set_row_height`
Hello, Thanks for adding your code, In version
Cheers |
No worries, looks good, will give them a try. Thank you |
Hello,
I have been trying a lot but I can't seem to make this happen reliably,
do you have any tips/ideas?
The text was updated successfully, but these errors were encountered: