0

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

5
  • 1
    It is hard to identify the issue without a minimal reproducible example and a screenshot. Commented Nov 28, 2024 at 1:02
  • Hi acw1668, sorry, im pretty new here, ive just added more code when im using the autofit and images for the ouctome Commented Nov 28, 2024 at 14:19
  • It is not clear what you mean by "tabview is or is not active". The difference between the two images is just that different tab is selected in the most inner tabview. Also we cannot copy your code and execute it because there are many missing parts. You need to provide a sample data instead of getting values from unknown sources. Commented Nov 28, 2024 at 14:37
  • I have a button that populates two diffrent treeviews in two diffrent tabviews.. when i click the button to populate, the tab that im current active, the autofit doesnt work, but the other one, that receives the same data as the active one, the outfit works properly. Commented Nov 28, 2024 at 15:53
  • Try adding stretch=0 to treeview.column(column, width=final_width, anchor='center') inside adjust_column_widths(). Commented Nov 29, 2024 at 8:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.