Skip to content

Alignment of special UTF-8 characters on labels #292

@matheuskknd

Description

@matheuskknd

Hello again, how are you doing?

I'm not sure if I should open an issue since I basically have a question.

It turns out that we are trying to add special UTF-8 characters to the labels as in the example below, but visually the text sometimes becomes misaligned as you can see in the print. I would like to know if there is any way to make the special characters align one below the other and also to make the texts aligned to the left between the labels.

Image

Python 3.8.12 example:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import weakref
from tkinter import messagebox, simpledialog
from typing import Any, Callable, Literal, cast

import customtkinter as ctk
import tksheet


class TableFrame(ctk.CTkFrame):

	def __init__(self, master: Any, **kwargs: Any) -> None:
		super().__init__(master, **kwargs)

		# Creating the spreadsheet as an attribute
		self.sheet: tksheet.Sheet = tksheet.Sheet(
			self,
			headers=["A", "B"],
			data=[['1', '2'], ['3', '4']],
		)
		self.sheet.pack(expand=True, fill="both")
		self.sheet.enable_bindings("all")

		# Creating a highlighted readonly span
		span: tksheet.Span = self.sheet.span(0, 1, 2, 2)
		span.highlight(bg="green", fg="black", redraw=False)
		span.readonly(True)

		# Labels translation
		self.sheet.set_options(
			copy_label="🗐 Copy",
			paste_label="📋 Paste",
			edit_cell_label="✏️ Edit",
			delete_label="🗑️ Delete",
			cut_label="✄ Cut",
			sort_rows_label="⬇ Sort Asc.",
			sort_rows_reverse_label="⬆ Sort Desc.",
			delete_rows_label="🗑️ Delete row(s)",
			insert_rows_above_label="➕ Add 1 row above",
			insert_rows_below_label="➕ Add 1 row bellow",
			insert_row_label="➕ Insert 1 row",
			clear_contents_label="✖️ Clear",
			copy_contents_label="🗐 Copy",
			cut_contents_label="✄ Cut",
			undo_label="⮌ Undo",
			select_all_label="⤫ Select All",
		)

		# Allows user to insert or add N rows in the sheet
		self.sheet.popup_menu_add_command(
			label="➕ Insert N row(s)",
			func=self.__create_insert_or_add_rows_command("INSERT", title="Insert row(s)",
																										button_label="Insert"),
			table_menu=False,
			index_menu=False,
			header_menu=False,
			empty_space_menu=True,
		)

		self.sheet.popup_menu_add_command(
			label="➕ Insert N row(s) above",
			func=self.__create_insert_or_add_rows_command("ADD_ABOVE", title="Add row(s) above",
																										button_label="Add above"),
			table_menu=False,
			index_menu=True,
			header_menu=False,
			empty_space_menu=False,
		)

		self.sheet.popup_menu_add_command(
			label="➕ Insert N row(s) bellow",
			func=self.__create_insert_or_add_rows_command("ADD_BELLOW", title="Add row(s) bellow",
																										button_label="Add bellow"),
			table_menu=False,
			index_menu=True,
			header_menu=False,
			empty_space_menu=False,
		)

	def __create_insert_or_add_rows_command(self, type_: Literal["ADD_ABOVE", "ADD_BELLOW", "INSERT"],
																					*, title: str, button_label: str) -> Callable[[], None]:
		w_sheet: tksheet.Sheet = weakref.proxy(self.sheet)

		def insert_or_add_rows_command() -> None:
			idx: int | None = None
			if type_.startswith("ADD"):
				selected: set[str] = cast(set[str], w_sheet.get_selected_rows())
				idx = int(next(iter(selected))) + int(type_ == "ADD_BELLOW")

			while True:
				text: str | None = simpledialog.askstring(title, button_label)
				if not text:
					return

				try:
					num_rows: int = int(text)
					if num_rows > 0:
						w_sheet.insert_rows(rows=num_rows, idx=idx, tree=False, redraw=True)
					return

				except Exception as e:
					messagebox.showwarning(f"Invalid number", str(e), parent=w_sheet)

		return insert_or_add_rows_command


def main():
	ctk.set_appearance_mode("light")
	ctk.set_default_color_theme("blue")

	root = ctk.CTk()
	root.geometry("800x400")
	root.title("customtkinter + tksheet")

	tabela = TableFrame(root)
	tabela.pack(expand=True, fill="both", padx=10, pady=10)

	root.mainloop()


if __name__ == "__main__":
	main()

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions