I'm trying to make a 'autofit' column width and the code below works really fine, but only if Im not in the tabview that the autofit occurs. Is there anyway to workaround this?
from tkinter import ttk, Font
def adjust_column_widths(treeview, columns, tabview):
try:
font = Font()
for column in columns:
# Calculate the width of the column title
title_width = font.measure(column)
# Calculate the maximum width of the column content
content_width = max(
(font.measure(treeview.set(item, column)) for item in treeview.get_children('')),
default=0
)
# Set the final width of the column
final_width = max(title_width, content_width) # Add padding for clarity
treeview.column(column, width=final_width, anchor='center')
except Exception as e:
print(f"Error adjusting column widths: {e}")
the code below is when im use the 'autofit'
def populate_option_file(tabview, tab, existing_tabs):
# Check if the "Monitor" tab already exists
if "Arquivo B3" not in existing_tabs:
# Initialize the operations monitor and get the tabviews
file_b3(tabview, tab, existing_tabs)
existing_tabs.append("Arquivo B3")
# Extract necessary data from the tables
commodities_ric, commodities_factor, cntpy_taxid, cntpy_acronym = extract_table_data()
def rept(char, count):
return char * count
# Process data for tabele_option_client
for i, item in enumerate(table_fileoption_client.get_children(), start=3):
values = table_fileoption_client.item(item, 'values')
market = values[2]
strike = values[6]
intermediate_ccy = values[8]
cntpy = values[18]
premiumperunit = values[19]
strike_result = strike_formula_option(market, strike, intermediate_ccy, commodities_ric, commodities_factor, tabview)
cnpj_cliente = lookup(cntpy, cntpy_acronym, cntpy_taxid)
decimal_strike_result = decimal_formula_option(market, strike, intermediate_ccy, commodities_ric, commodities_factor, tabview)
base_value = int(values[9].replace(",", "").replace("-", ""))
pu_result = pu_formula_option(market, premiumperunit, intermediate_ccy, commodities_ric, commodities_factor, tabview)
pu_decimal_result = pudecimal_formula_option(market, premiumperunit, intermediate_ccy, commodities_ric, commodities_factor, tabview)
line = [
"OPTION",
"1",
"0002",
"OFCV" if values[4] == "Option (Put)" else "OFCC",
values[23],
"00001",
"0000",
"P2" if values[3] == "Sell" else "P1",
rept(" ", 3),
safe_date_conversion(values[1]),
safe_date_conversion(values[10]),
strike_result,
decimal_strike_result,
pu_result,
pu_decimal_result,
rept(" ", 8),
]
table_fileoption_client.insert("", "end", values=line)
adjust_column_widths(table_fileoption_client, columns_option_file, tabview)
[1]:[when the tabview is the active]https://i.sstatic.net/f5IOTNv6.png
[2]:[when the tabview is not the active] https://i.sstatic.net/tCksiDMy.png
stretch=0totreeview.column(column, width=final_width, anchor='center')insideadjust_column_widths().