it does not sho any menu or button in the gui for changing the datatype please change…
- User
it does not sho any menu or button in the gui for changing the datatype please change this: import tkinter as tk from tksheet import Sheet, int_formatter, float_formatter, percentage_formatter, bool_formatter, num2alpha
class DemoApp(tk.Tk): def init(self): super().init() self.title('tksheet Data Formatting Demo') self.geometry('800x600')
# Sample data representing different data types as strings data = [ ["123", "45.67", "89%", "True"], # Row 0 ["456", "78.90", "12%", "False"], # Row 1 ["789", "12.34", "34%", "Yes"], # Row 2 ["abc", "invalid", "not a percent", "No"], # Invalid data ] # Create tksheet instance self.sheet = Sheet(self, data=data) self.sheet.enable_bindings("all") self.sheet.pack(expand=True, fill='both') # Set column headers self.sheet.headers([ "Integer Column", "Float Column", "Percentage Column", "Boolean Column", ]) # Apply default formatters self.apply_default_formatters() # Bind right-click event on the sheet self.sheet.bind("<Button-3>", self.right_click_event, add='+') def apply_default_formatters(self): # Apply integer formatter to column A (index 0) self.sheet[num2alpha(0)].format(int_formatter()) # Apply float formatter to column B (index 1) self.sheet[num2alpha(1)].format(float_formatter()) # Apply percentage formatter to column C (index 2) self.sheet[num2alpha(2)].format(percentage_formatter()) # Apply boolean formatter to column D (index 3) self.sheet[num2alpha(3)].format(bool_formatter()) def right_click_event(self, event): # Identify the region where the click occurred region = self.sheet.identify_region(event) if region == "header": # Get the column index where the click occurred col_index = self.sheet.identify_column(event) # Show the format selection menu self.show_format_menu(event, col_index) def show_format_menu(self, event, col_index): # Create a popup menu menu = tk.Menu(self, tearoff=0) # Add format options menu.add_command(label="Format as Integer", command=lambda: self.format_column_as(col_index, 'int')) menu.add_command(label="Format as Float", command=lambda: self.format_column_as(col_index, 'float')) menu.add_command(label="Format as Percentage", command=lambda: self.format_column_as(col_index, 'percentage')) menu.add_command(label="Format as Boolean", command=lambda: self.format_column_as(col_index, 'bool')) menu.add_separator() menu.add_command(label="Remove Format", command=lambda: self.remove_column_format(col_index)) # Display the menu at the cursor position menu.tk_popup(event.x_root, event.y_root) def format_column_as(self, col_index, format_type): # Convert column index to column letter (e.g., 0 -> 'A') col_letter = num2alpha(col_index) # Apply the selected formatter to the column if format_type == 'int': self.sheet[col_letter].format(int_formatter()) elif format_type == 'float': self.sheet[col_letter].format(float_formatter()) elif format_type == 'percentage': self.sheet[col_letter].format(percentage_formatter()) elif format_type == 'bool': self.sheet[col_letter].format(bool_formatter()) # Refresh the sheet to reflect changes self.sheet.redraw() def remove_column_format(self, col_index): # Convert column index to column letter col_letter = num2alpha(col_index) # Remove the formatter from the column self.sheet[col_letter].del_format() # Refresh the sheet to reflect changes self.sheet.redraw() - Assistant