Created
December 25, 2023 19:08
-
-
Save secemp9/eb935b4ddb44e123cf40ddbb6375d23e to your computer and use it in GitHub Desktop.
tkinter tabs editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| from tkinter import ttk | |
| from tkinter import NE, LEFT, RIGHT | |
| class ScrollableNotebook(ttk.Frame): | |
| def __init__(self, parent, *args, **kwargs): | |
| ttk.Frame.__init__(self, parent, *args) | |
| self.xLocation = 0 | |
| self.notebookContent = ttk.Notebook(self, **kwargs) | |
| self.notebookContent.pack(fill="both", expand=True) | |
| self.notebookTab = ttk.Notebook(self, **kwargs) | |
| self.notebookTab.bind("<<NotebookTabChanged>>", self._tabChanger) | |
| slideFrame = ttk.Frame(self) | |
| slideFrame.place(relx=1.0, x=0, y=1, anchor=NE) | |
| leftArrow = ttk.Label(slideFrame, text="\u25c0") | |
| leftArrow.bind("<1>", self._leftSlide) | |
| leftArrow.pack(side=LEFT) | |
| rightArrow = ttk.Label(slideFrame, text=" \u25b6") | |
| rightArrow.bind("<1>", self._rightSlide) | |
| rightArrow.pack(side=RIGHT) | |
| self.notebookContent.bind("<Configure>", self._resetSlide) | |
| def _tabChanger(self, event): | |
| self.notebookContent.select(self.notebookTab.index("current")) | |
| def _rightSlide(self, event): | |
| if self.notebookTab.winfo_width() > self.notebookContent.winfo_width() - 30: | |
| if (self.notebookContent.winfo_width() - (self.notebookTab.winfo_width() + self.notebookTab.winfo_x())) <= 35: | |
| self.xLocation -= 20 | |
| self.notebookTab.place(x=self.xLocation, y=0) | |
| def _leftSlide(self, event): | |
| if not self.notebookTab.winfo_x() == 0: | |
| self.xLocation += 20 | |
| self.notebookTab.place(x=self.xLocation, y=0) | |
| def _resetSlide(self, event): | |
| self.notebookTab.place(x=0, y=0) | |
| self.xLocation = 0 | |
| def add(self, frame, **kwargs): | |
| if len(self.notebookTab.winfo_children()) != 0: | |
| self.notebookContent.add(frame, text="", state="hidden") | |
| else: | |
| self.notebookContent.add(frame, text="") | |
| self.notebookTab.add(ttk.Frame(self.notebookTab), **kwargs) | |
| def forget(self, tab_id): | |
| self.notebookContent.forget(tab_id) | |
| self.notebookTab.forget(tab_id) | |
| def hide(self, tab_id): | |
| self.notebookContent.hide(tab_id) | |
| self.notebookTab.hide(tab_id) | |
| def identify(self, x, y): | |
| return self.notebookTab.identify(x, y) | |
| def index(self, tab_id): | |
| return self.notebookTab.index(tab_id) | |
| def insert(self, pos, frame, **kwargs): | |
| self.notebookContent.insert(pos, frame, **kwargs) | |
| self.notebookTab.insert(pos, frame, **kwargs) | |
| def select(self, tab_id): | |
| self.notebookContent.select(tab_id) | |
| self.notebookTab.select(tab_id) | |
| def tab(self, tab_id, option=None, **kwargs): | |
| return self.notebookTab.tab(tab_id, option=None, **kwargs) | |
| def tabs(self): | |
| return self.notebookContent.tabs() | |
| def enable_traversal(self): | |
| self.notebookContent.enable_traversal() | |
| self.notebookTab.enable_traversal() | |
| tab_count = 1 | |
| def create_tab(): | |
| global tab_count # Access the global tab_count variable | |
| tab = ttk.Frame(notebook.notebookContent) | |
| notebook.add(tab, text=f"<Untitled> {tab_count}") # Use tab_count in the tab text | |
| text_widget = tk.Text(tab) | |
| text_widget.pack(fill="both", expand=True) | |
| notebook.notebookContent.select(tab) | |
| text_widget.focus_set() | |
| tab_count += 1 # Increment the tab_count | |
| def switch_tab(event=None): | |
| current_tab = notebook.notebookContent.select() | |
| notebook.notebookContent.select(current_tab) | |
| current_tab_widget = notebook.notebookContent.nametowidget(current_tab) | |
| text_widget = current_tab_widget.winfo_children()[0] | |
| text_widget.focus_set() | |
| root = tk.Tk() | |
| root.title("Text Editor") | |
| frame = ttk.Frame(root) | |
| frame.pack(fill="both", expand=True) | |
| notebook = ScrollableNotebook(frame) | |
| notebook.pack(fill="both", expand=True) | |
| create_tab() | |
| add_button = ttk.Button(frame, text="New Tab", command=create_tab) | |
| add_button.pack(side="bottom") | |
| notebook.notebookContent.bind("<ButtonRelease-1>", switch_tab) | |
| root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment