ragardner / tksheet

Python tkinter table widget for displaying tabular data
https://pypi.org/project/tksheet/
MIT License
408 stars 50 forks source link

How to fix the maximum size of characters in tksheet ceels #208

Closed janez111 closed 9 months ago

janez111 commented 10 months ago

The idea is as follows, i am creating GUI with tksheet tables for data input. I would like to prevent user to write down more than 12 characters in single tksheet cell. So if it is more than 12 characters the end ones shall be trimmed. Here is part of my code, i hope that is relevant.

preglednica_ref.set_sheet_data([['' for i in range(6)] for j in range(20)], reset_col_positions=True) column_widths_ref = [150 width_factor, 127 width_factor, 127 width_factor, 127 width_factor, 127 width_factor, 127 width_factor] preglednica_ref.set_column_widths(column_widths_ref)

P.S.: I have another question is there maybe a simple way to translate following actions into other languages: cut, copy, paste,...?

Best regards, Janez

ragardner commented 9 months ago

Hello,

Thanks for your excellent questions and sorry for the delay in my reply

I would like to prevent user to write down more than 12 characters in single tksheet cell. So if it is more than 12 characters the end ones shall be trimmed.

Unfortunately with tksheet versions 6.x.x this is rather difficult and verbose so I've made an effort to finish version 7 and changed it's behaviour a little

Here is the changelog for version 7 in case you're interested:

And here is the code I've made to demonstrate how easy this is with versions 7+

All edits including paste should be trimmed to 12 characters

The documentation for the function is available here:

from tksheet import Sheet
import tkinter as tk

class demo(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)

        # create an instance of Sheet()
        self.sheet = Sheet(
            # set the Sheets parent widget
            self,
            # optional: set the Sheets data at initialization
            data=[[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(20)] for r in range(100)],
            theme="light green",
            height=520,
            width=1000,
        )
        # enable various bindings
        self.sheet.enable_bindings()

        # set a user edit validation function
        # AND bind all sheet modification events to a function
        # chained as two functions
        # more information at:
        # https://github.com/ragardner/tksheet/wiki/Version-7#validate-user-cell-edits
        self.sheet.edit_validation(self.validate_edits)
        self.sheet.grid(row=0, column=0, sticky="nswe")

    def validate_edits(self, event):
        # trim the edits to the table
        return event.value[:12]

app = demo()
app.mainloop()

For your 2nd question

P.S.: I have another question is there maybe a simple way to translate following actions into other languages: cut, copy, paste,...?

I am not sure, perhaps in your code you could create links to the functions using variables/attributes

# i think this is copy in spanish
sheet_copiar = sheet.copy

# or...

# i think this is copy in swedish
sheet_kopiera = sheet.copy

and then using the function later on:

sheet_kopiera()

Let me know if you run into any issues

Kind regards

janez111 commented 9 months ago

Dear sir,

thanks for the response. I will try your proposed translation tip. In meantime i somehow solve the issue with trimming, so below is my code, to trim first column to max 16 signs and all the others to max 11 signs.

def obrezi_podatke_za_reference(event): cell_value = preglednica_ref.get_cell_data(event.row, event.column) max_length = 16 if event.column == 0 else 11 if len(cell_value) > max_length: trimmed_value = cell_value[:max_length] preglednica_ref.set_cell_data(event.row, event.column, trimmed_value, redraw=True) preglednica_ref.set_sheet_data([['' for i in range(6)] for j in range(20)], reset_col_positions=True)preglednica_ref.extra_bindings(['end_edit_cell'], func=obrezi_podatke_za_reference)

and in preglednica_ref.set_options, the following setting shall be applied: edit_cell_validation = False

Best regards, Janez

Sent with Proton Mail secure email.

On Thursday, January 25th, 2024 at 6:07 PM, ragardner @.***> wrote:

Hello,

Thanks for your excellent questions and sorry for the delay in my reply

I would like to prevent user to write down more than 12 characters in single tksheet cell. So if it is more than 12 characters the end ones shall be trimmed.

Unfortunately with tksheet versions 6.x.x this is rather difficult and verbose so I've made an effort to finish version 7 and changed it's behaviour a little

Here is the changelog for version 7 in case you're interested:

And here is the code I've made to demonstrate how easy this is with versions 7+

All edits including paste should be trimmed to 12 characters

The documentation for the function is available here:

from

tksheet

import

Sheet

import

tkinter

as

tk

class

demo

(

tk

.

Tk

):

def

init

(

self

):

tk

.

Tk

.

init

(

self

)

self

.

grid_columnconfigure

(

0

,

weight

=

1

)

self

.

grid_rowconfigure

(

0

,

weight

=

1

)

create an instance of Sheet()

self

.

sheet

=

Sheet

(

set the Sheets parent widget

self

,

optional: set the Sheets data at initialization

data

=

[[

f"Row

{

r

}

, Column

{

c

}

\n

newline1

\n

newline2"

for

c

in

range

(

20

)]

for

r

in

range

(

100

)],

theme

=

"light green"

,

height

=

520

,

width

=

1000

, )

enable various bindings

self

.

sheet

.

enable_bindings

()

set a user edit validation function

AND bind all sheet modification events to a function

chained as two functions

more information at:

https://github.com/ragardner/tksheet/wiki/Version-7#validate-user-cell-edits

self

.

sheet

.

edit_validation

(

self

.

validate_edits

)

self

.

sheet

.

grid

(

row

=

0

,

column

=

0

,

sticky

=

"nswe"

)

def

validate_edits

(

self

,

event

):

trim the edits to the table

return

event

.

value

[:

12

]

app

=

demo

()

app

.

mainloop

()

For your 2nd question

P.S.: I have another question is there maybe a simple way to translate following actions into other languages: cut, copy, paste,...?

I am not sure, perhaps in your code you could create links to the functions using variables/attributes

i think this is copy in spanish

sheet_copiar

=

sheet

.

copy

or...

i think this is copy in swedish

sheet_kopiera

=

sheet

.

copy

and then using the function later on:

sheet_kopiera

()

Let me know if you run into any issues

Kind regards

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

janez111 commented 9 months ago

Hi,

Probably my last question about translation was to general, so the idea was to translate the menu which opens when You right-click one of the tksheet ceels. So far i got some partial success with such code:

def nastavi_tksheet_enabled(tk_sheet): tk_sheet.enable_bindings((('single_select',

'drag_select',

'column_drag_and_drop',

'row_drag_and_drop',

'column_select', 'row_select',

'column_width_resize',

'double_click_column_resize',

'arrowkeys',

'row_height_resize',

'double_click_row_resize',

'right_click_popup_menu',

'rc_select',

'rc_insert_column',

'rc_delete_column',

'rc_insert_row',

'rc_delete_row',

'copy',

'cut',

'paste',

'delete',

'undo',

'edit_cell',

)))

tk_sheet.popup_menu_add_command('Kopiraj', tk_sheet.copy) tk_sheet.popup_menu_add_command('Izreži', tk_sheet.cut) tk_sheet.popup_menu_add_command('Prilepi', tk_sheet.paste) tk_sheet.popup_menu_add_command('Izbriši', tk_sheet.delete) tk_sheet.popup_menu_add_command('Uredi celico', tk_sheet.cell_edit_binding(enable = True))

tk_sheet.set_dropdown_values(set_existing_dropdown=True, values=['Kopiraj', 'Izreži', 'Prilepi', 'Uredi celico']) """

The code works for copy, paste, delete, the issue is with 'edit_cell' which even though that is commented in tk_sheet.enable_bindings it still persist in menu, In fact there i got both 'Edit cell' and its Slovenian equivalent 'Uredi celico'?. The last row #tk_sheet.set_dropdown_values(set_existing_dropdown=True, values=['Kopiraj', 'Izreži', 'Prilepi', 'Uredi celico']) is commented as it generates me the error. I am wondering whether my idea is plausible or I should do something completely different?

Best regards, Janez

Sent with Proton Mail secure email.

On Thursday, January 25th, 2024 at 9:57 PM, Janez Bernard @.***> wrote:

Dear sir,

thanks for the response. I will try your proposed translation tip. In meantime i somehow solve the issue with trimming, so below is my code, to trim first column to max 16 signs and all the others to max 11 signs.

def obrezi_podatke_za_reference(event): cell_value = preglednica_ref.get_cell_data(event.row, event.column) max_length = 16 if event.column == 0 else 11 if len(cell_value) > max_length: trimmed_value = cell_value[:max_length] preglednica_ref.set_cell_data(event.row, event.column, trimmed_value, redraw=True) preglednica_ref.set_sheet_data([['' for i in range(6)] for j in range(20)], reset_col_positions=True)preglednica_ref.extra_bindings(['end_edit_cell'], func=obrezi_podatke_za_reference)

and in preglednica_ref.set_options, the following setting shall be applied: edit_cell_validation = False

Best regards, Janez

Sent with Proton Mail secure email.

On Thursday, January 25th, 2024 at 6:07 PM, ragardner @.***> wrote:

Hello,

Thanks for your excellent questions and sorry for the delay in my reply

I would like to prevent user to write down more than 12 characters in single tksheet cell. So if it is more than 12 characters the end ones shall be trimmed.

Unfortunately with tksheet versions 6.x.x this is rather difficult and verbose so I've made an effort to finish version 7 and changed it's behaviour a little

Here is the changelog for version 7 in case you're interested:

And here is the code I've made to demonstrate how easy this is with versions 7+

All edits including paste should be trimmed to 12 characters

The documentation for the function is available here:

from

tksheet

import

Sheet

import

tkinter

as

tk

class

demo

(

tk

.

Tk

):

def

init

(

self

):

tk

.

Tk

.

init

(

self

)

self

.

grid_columnconfigure

(

0

,

weight

=

1

)

self

.

grid_rowconfigure

(

0

,

weight

=

1

)

create an instance of Sheet()

self

.

sheet

=

Sheet

(

set the Sheets parent widget

self

,

optional: set the Sheets data at initialization

data

=

[[

f"Row

{

r

}

, Column

{

c

}

\n

newline1

\n

newline2"

for

c

in

range

(

20

)]

for

r

in

range

(

100

)],

theme

=

"light green"

,

height

=

520

,

width

=

1000

, )

enable various bindings

self

.

sheet

.

enable_bindings

()

set a user edit validation function

AND bind all sheet modification events to a function

chained as two functions

more information at:

https://github.com/ragardner/tksheet/wiki/Version-7#validate-user-cell-edits

self

.

sheet

.

edit_validation

(

self

.

validate_edits

)

self

.

sheet

.

grid

(

row

=

0

,

column

=

0

,

sticky

=

"nswe"

)

def

validate_edits

(

self

,

event

):

trim the edits to the table

return

event

.

value

[:

12

]

app

=

demo

()

app

.

mainloop

()

For your 2nd question

P.S.: I have another question is there maybe a simple way to translate following actions into other languages: cut, copy, paste,...?

I am not sure, perhaps in your code you could create links to the functions using variables/attributes

i think this is copy in spanish

sheet_copiar

=

sheet

.

copy

or...

i think this is copy in swedish

sheet_kopiera

=

sheet

.

copy

and then using the function later on:

sheet_kopiera

()

Let me know if you run into any issues

Kind regards

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

ragardner commented 9 months ago

Hello,

Probably my last question about translation was to general, so the idea was to translate the menu which opens when You right-click one of the tksheet ceels.

Oh sorry I misunderstood, yea that's a good idea. Currently you'd have to edit the tksheet code by going to the tksheet folder then if you're using version 6.x.x inside the file _tksheet_main_table.py and starting on line 2262 - label="Edit header", edit each of the label= lines to your desired labels

You'll also have to go into the file _tksheet_other_classes.py and starting on line 116 - label="Select all", and edit those label= lines also

I am working on a way of setting these things but the change won't be out for version 6, only for version 7 and I can't give a date on it

The last row #tk_sheet.set_dropdown_values(set_existing_dropdown=True, values=['Kopiraj', 'Izreži', 'Prilepi', 'Uredi celico']) is commented as it generates me the error. I am wondering whether my idea is plausible or I should do something completely different?

I am not sure if this is your issue but only use set_existing_dropdown=True if there is a dropdown open at the time otherwise use the r and c parameters to set a specific dropdowns values

Hope this helps

janez111 commented 9 months ago

Hi,

yes, everything is clear to me now. Nevertheless, I have decided to keep the menu in English; the Slovenian language wouldl be just a nice final touch, nothing more.

Once again, I would like to express my gratitude for contribution of tksheet and for helping me.

Best regards, Janez

Sent with Proton Mail secure email.

On Saturday, January 27th, 2024 at 6:58 PM, ragardner @.***> wrote:

Hello,

Probably my last question about translation was to general, so the idea was to translate the menu which opens when You right-click one of the tksheet ceels.

Oh sorry I misunderstood, yea that's a good idea. Currently you'd have to edit the tksheet code by going to the tksheet folder then if you're using version 6.x.x inside the file _tksheet_main_table.py and starting on line 2262 - label="Edit header", edit each of the label= lines to your desired labels

You'll also have to go into the file _tksheet_other_classes.py and starting on line 116 - label="Select all", and edit those label= lines also

I am working on a way of setting these things but the change won't be out for version 6, only for version 7 and I can't give a date on it

The last row #tk_sheet.set_dropdown_values(set_existing_dropdown=True, values=['Kopiraj', 'Izreži', 'Prilepi', 'Uredi celico']) is commented as it generates me the error. I am wondering whether my idea is plausible or I should do something completely different?

I am not sure if this is your issue but only use set_existing_dropdown=True if there is a dropdown open at the time otherwise use the r and c parameters to set a specific dropdowns values

Hope this helps

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

ragardner commented 9 months ago

Thank you for the reply and for giving me new ideas for the project! If you're interested and also using the latest version I have added the label change functionality:

https://github.com/ragardner/tksheet/wiki/Version-7#sheet-languages-and-bindings