TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.33k stars 1.07k forks source link

Notebook Binding is not working #2143

Open f1r3nz4r opened 9 months ago

f1r3nz4r commented 9 months ago

Here's the code I wrote: ` import customtkinter as ctk

class Root: def init(self): self.root = ctk.CTk() ctk.set_appearance_mode("system")

    self.tabViewInit()

def tabViewInit(self):
    self.tabView = ctk.CTkTabview(self.root)
    self.tabView.pack(fill=ctk.BOTH, expand=ctk.YES)

    def on_click(event):
        clickedTab = self.tabView.tk.call(self.tabView._w, "identify", "tab", event.x, event.y)
        if clickedTab != 0:
            self.tabView.forget(clickedTab)

    self.tabView.bind('<button 3>', on_click)

def runLoop(self):
    self.root.mainloop()

if name == "main": root = Root() root.runLoop() `

Here I can create a tabview but the bind method throws a NotImplementedError. Here's what the terminal throws at me.

Traceback (most recent call last): File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 29, in <module> root = Root() ^^^^^^ File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 9, in __init__ self.tabViewInit() File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 20, in tabViewInit self.tabView.bind('<button 3>', on_click) File "C:\Users\anees\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\core_widget_classes\ctk_base_class.py", line 244, in bind raise NotImplementedError NotImplementedError

Please help me resolve this issue at the earliest.

LorenzoMattia commented 9 months ago

Hi @f1r3nz4r, could you please explain what you are trying to do?

However, a problem could probably be that the binding command should be

self.tabView.bind('<Button-3>', on_click)

and not

self.tabView.bind('<button 3>', on_click)
f1r3nz4r commented 9 months ago

So I'm creating a UI which uses tabs to open up new "pages". The class that I've created allows easy creation of a tabview alongwith some methods to add and destroy tabs, for the latter which I faced issues.

And I also checked the docs after ur suggestion and it seems there was a typo in the bind method. I'm sorry for disturbing you for such trivial mistakes. I also thank you for your prompt response.

f1r3nz4r commented 9 months ago

So I'm creating a UI which uses tabs to open up new "pages". The class that I've created allows easy creation of a tabview alongwith some methods to add and destroy tabs, for the latter which I faced issues.

And I also checked the docs after ur suggestion and it seems there was a typo in the bind method. I'm sorry for distributing you for such trivial mistakes. I also thank you for your prompt response.

LorenzoMattia commented 9 months ago

Don't worry! Close the issue if you have solved your problem!

f1r3nz4r commented 9 months ago

@LorenzoMattia I changed the "<button 3>" in the bind method to "" but it still does not work. Here's the traceback: Traceback (most recent call last): File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 29, in <module> root = Root() ^^^^^^ File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 9, in __init__ self.tabViewInit() File "d:\Work Related\GhPM\Changes 3\UI_Functions\UI_Builder\customnotebook.py", line 20, in tabViewInit self.tabView.bind('<Button-3>', on_click) File "C:\Users\anees\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\core_widget_classes\ctk_base_class.py", line 244, in bind raise NotImplementedError NotImplementedError

LorenzoMattia commented 9 months ago

Hi @f1r3nz4r, sorry for the late answer. Now I see the problem. You are trying to bind an event to the right click on the tabview. However, this is not possible. CtkTabview inherits from CTkBaseClass, whose bind method is implement as:

def bind(self, sequence=None, command=None, add=None):
        raise NotImplementedError

So this is why you are getting that error.

What you can actually do is binding the right click to a tab already created inside the tabview doing something like:

tabView = customtkinter.CTkTabview(self)
tabView.pack(expand = True)
new_tab = tabView.add("New Tab")
def example(event = None):
    # code
    pass
new_tab.bind("<Button-3>", command=example)

Alternatively, if your goal is to allow rapid creation and cancellation of tabs you can bind your root to a certain shortcut like:

self.root.bind("<Control-n>", new_tab_creation)

where the new_tab_creation method is responsible of creating a new tab, and the same can be done for cancellation.

Let me know if I can further help you!

straight-code commented 9 months ago

    self.tabview = customtkinter.CTkTabview(self, width=250)
    self.tabview.pack(expand=True, fill='both', padx=(20, 20), pady=(20, 20))  # Use pack instead of grid

    self.tabview.add("Whats FREE")

    self.label_tab_2 = customtkinter.CTkLabel(self.tabview.tab("Whats FREE"), text="Whats FREE? section ")
    self.label_tab_2.grid(row=0, column=0, padx=20, pady=20)

    self.combobox_1 = customtkinter.CTkComboBox(self.tabview.tab("Whats FREE"),
                                                values=["Value 1", "Value 2", "Value Long....."])
    self.combobox_1.grid(row=1, column=0, padx=20, pady=(10, 10))

having no issue when appoarching it from this way. Hope this helps :) @f1r3nz4r