Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question #17

Closed
shreyash-sharma opened this issue Apr 22, 2020 · 3 comments
Closed

Question #17

shreyash-sharma opened this issue Apr 22, 2020 · 3 comments

Comments

@shreyash-sharma
Copy link

Thank you for making this! I just had a question, If I wanted to disable the right click menu and add a custom button to delete the entire row.
How will I proceed?
I am quite new to tkinter.

@ragardner
Copy link
Owner

ragardner commented Apr 24, 2020

It's a bit crude but

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)
        self.frame = tk.Frame(self)
        self.frame.grid_columnconfigure(0, weight = 1)
        self.frame.grid_rowconfigure(0, weight = 1)
        self.sheet = Sheet(self.frame,
                           data = [[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(30)] for r in range(2000)], #to set sheet data at startup
                           headers = [f"Column {c}\nnewline1\nnewline2" for c in range(30)],
                            )
        self.sheet.enable_bindings(("single_select", #"single_select" or "toggle_select"
                                     "drag_select",   #enables shift click selection as well
                                    "rc_select",
                                    "row_select",
                                    "column_select",
                                     "arrowkeys",
                                     "row_height_resize",
                                     "double_click_row_resize",
                                     "copy",
                                     "cut",
                                     "paste",
                                     "delete",
                                     "undo",
                                     "edit_cell"))
        self.frame.grid(row = 0, column = 0, sticky = "nswe")
        self.sheet.grid(row = 0, column = 0, sticky = "nswe")
        self.del_rows_button = tk.Button(self,
                                         text = "Delete Rows",
                                         command = self.del_rows)
        self.del_rows_button.grid(row = 1, column = 0)
        self.new_rc_popup_menu = tk.Menu(self, tearoff = 0, background = "white")
        self.new_rc_popup_menu.add_command(label = "Delete Rows",
                                             font = ("Arial", 13, "normal"),
                                             foreground = "gray10",
                                             background = "white",
                                             activebackground = "gray90",
                                             activeforeground = "gray5",
                                             command = self.del_rows)
        #self.sheet.bind("<3>", self.rc) #use "<2>" if on Mac OS this is in case you need your own right click binding

    def rc(self, event):
        self.new_rc_popup_menu.tk_popup(event.x_root, event.y_root)

    def del_rows(self, event = None):
        selected_rows = self.sheet.get_selected_rows()
        if selected_rows:
            start = min(selected_rows)
            end = max(selected_rows) + 1
            self.sheet.MT.data_ref[start:end] = []
            index = self.sheet.row_index()
            if index:
                try:
                    index[start:end] = []
                    self.sheet.row_index(index)
                except:
                    pass
            self.sheet.deselect("all")
            self.sheet.refresh()


app = demo()
app.mainloop()

You can control the bindings using enable_bindings() function for the Sheet() widget, all the bindings are listed in the readme file demonstration

@ragardner ragardner reopened this Apr 24, 2020
@ragardner
Copy link
Owner

@CorpseKiller I reopened to update my response, I am not sure if you mean your own right click function or literally a button, but I put both in the example

@shreyash-sharma
Copy link
Author

Thanks a lot for taking the time good sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants