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)