0

Description: I'm currently working on a NotepadApp using Tkinter, where I have implemented a sidebar with buttons for common functions like "New," "Open," and "Save." Additionally, there are radio buttons to switch between light and dark themes. The app allows users to show or hide the sidebar using a "Hide Sidebar" button placed at the top.

The problem I'm facing is that after I hide the sidebar using the "Hide Sidebar" button, I'm unable to show it again using the same button. The sidebar frame disappears as expected, but when I click the button again to show the sidebar, the buttons and radio buttons do not reappear.

I've checked the toggle_sidebar() method, and it seems to be working correctly, hiding and showing the sidebar frame as intended. However, I suspect that the issue lies with the create_sidebar() method, as it may not be re-creating the sidebar buttons when the sidebar is shown again.

I'm looking for guidance on how to fix this bug and ensure that the sidebar buttons and radio buttons reappear when the "Show Sidebar" button is clicked after hiding the sidebar.

I have provided the relevant sections of my code below:

# Code for creating the sidebar
def create_sidebar(self):
    if hasattr(self, "new_button") and self.new_button.winfo_exists():
        return  # Sidebar buttons already exist, so no need to recreate them

    self.new_button = tk.Button(self.sidebar_frame, text="New", width=10, command=self.new_file)
    self.open_button = tk.Button(self.sidebar_frame, text="Open", width=10, command=self.open_file)
    self.save_button = tk.Button(self.sidebar_frame, text="Save", width=10, command=self.save_file)
    self.light_theme_radio = tk.Radiobutton(self.sidebar_frame, text="Light", variable=self.theme_mode, value="light", command=self.set_theme)
    self.dark_theme_radio = tk.Radiobutton(self.sidebar_frame, text="Dark", variable=self.theme_mode, value="dark", command=self.set_theme)

    self.new_button.pack(pady=5)
    self.open_button.pack(pady=5)
    self.save_button.pack(pady=5)
    self.light_theme_radio.pack(pady=5)
    self.dark_theme_radio.pack(pady=5)

# Code for toggling the sidebar
def toggle_sidebar(self):
    if self.show_sidebar:
        self.sidebar_frame.pack_forget()
        self.show_hide_button.config(text="Show Sidebar")
    else:
        self.sidebar_frame.pack(fill="y", side="left")
        self.show_hide_button.config(text="Hide Sidebar")
    self.show_sidebar = not self.show_sidebar

Any help or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

class NotepadApp:
    def __init__(self, root):
        self.root = root
        self.root.title("NoteS")
        self.theme_mode = tk.StringVar(value="dark")  # Initialize the default theme

        # Sidebar
        self.sidebar_frame = tk.Frame(root, width=150, bg="#EFEFEF")
        self.sidebar_frame.pack(fill="y", side="left")

        self.create_sidebar()

        # Text area
        self.text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD, font=("Arial", 12))
        self.text_area.pack(expand=True, fill='both')

        self.show_sidebar = True
        self.show_hide_button = tk.Button(root, text="Hide Sidebar", width=12, command=self.toggle_sidebar)
        self.show_hide_button.pack(pady=5, side="top")  # Specify the side to place the button at the top of the window.

        self.set_theme()

        self.cursor_color = "yellow"  # Default cursor color for dark theme
        self.blink_cursor()

    def create_sidebar(self):
        if hasattr(self, "new_button") and self.new_button.winfo_exists():
            return  # Sidebar buttons already exist, so no need to recreate them

        self.new_button = tk.Button(self.sidebar_frame, text="New", width=10, command=self.new_file)
        self.open_button = tk.Button(self.sidebar_frame, text="Open", width=10, command=self.open_file)
        self.save_button = tk.Button(self.sidebar_frame, text="Save", width=10, command=self.save_file)
        self.light_theme_radio = tk.Radiobutton(self.sidebar_frame, text="Light", variable=self.theme_mode, value="light", command=self.set_theme)
        self.dark_theme_radio = tk.Radiobutton(self.sidebar_frame, text="Dark", variable=self.theme_mode, value="dark", command=self.set_theme)

        self.new_button.pack(pady=5)
        self.open_button.pack(pady=5)
        self.save_button.pack(pady=5)
        self.light_theme_radio.pack(pady=5)
        self.dark_theme_radio.pack(pady=5)

    def toggle_sidebar(self):
        if self.show_sidebar:
            self.sidebar_frame.pack_forget()
            self.show_hide_button.config(text="Show Sidebar")
        else:
            self.sidebar_frame.pack(fill="y", side="left")
            self.show_hide_button.config(text="Hide Sidebar")
        self.show_sidebar = not self.show_sidebar

I tried using this code but failed and result was below image , if you need the code here is the [github repo] (https://github.com/praTeek271/Notes_application)

image

1 Answers1

0

The problem stems from the fact that the order in which you call pack matters. When you try to restore the sidebar, the only space available is below self.text_area since self.text_area was packed to the top (the default when you don't specify a side).

This is one area where grid might be a better choice than pack, because you can use grid_remove and it will be removed from the screen but the options (the row and column, etc) are remembered and will be restored the next time you call grid() on the same widget. pack doesn't have a similar feature.

However, you're using pack and there's a solution when using pack. You can use the before option to tell the packer to pack the sidebar before the text area in the packing order. It will be as if you literally called pack on the sidebar before you call it on the text area.

def toggle_sidebar(self):
    if self.show_sidebar:
        self.sidebar_frame.pack_forget()
        self.show_hide_button.config(text="Show Sidebar")
    else:
        self.sidebar_frame.pack(fill="y", side="left", before=self.text_area)
        #                                            ^^^^^^^^^^^^^^^^^^^^^^^
        self.show_hide_button.config(text="Hide Sidebar")
    self.show_sidebar = not self.show_sidebar

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685