I am currently working on a file manager, and I have implemented the ListView and IconView individually successfully. Now I wanted to create a toggle button so that user can toggle between those views. I thought of implementing this by changing the main view according to the state of toggle button:
if toggletoolbutton.get_active():
main_view = create_icon_view()
else:
main_view = create_list_view()
And for this, I created the event for toggle button: toggletoolbutton.connect("toggled", click_toggle_button)
def click_toggle_button(widget):
global main_view
print(toggletoolbutton.get_active())
print(main_view)
if(toggletoolbutton.get_active()):
populate_icon_store()
else:
populate_list_store()
if toggletoolbutton.get_active():
main_view = create_icon_view()
else:
main_view = create_list_view()
Here are my two stores:
icon_store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, bool, float, float)
list_store = Gtk.ListStore(str, str, bool, float, float)
And I am populating them using:
def get_icon(name):
theme = Gtk.IconTheme.get_default()
return theme.load_icon(name, 60, 0)
def populate_icon_store():
global icon_store
icon_store.clear()
file_icon = get_icon(Gtk.STOCK_FILE)
folder_icon = get_icon(Gtk.STOCK_DIRECTORY)
for file_name in os.listdir(CURRENT_DIRECTORY):
modified_time = 0#os.path.getmtime(CURRENT_DIRECTORY+file_name)
size = 0#os.path.getsize(CURRENT_DIRECTORY+file_name)
if not file_name[0] == '.':
if os.path.isdir(os.path.join(CURRENT_DIRECTORY, file_name)):
icon_store.append([
folder_icon,
file_name,
True,
modified_time,
size
])
else:
icon_store.append([
file_icon,
file_name,
False,
modified_time,
size
])
def populate_list_store():
global list_store
list_store.clear()
file_icon = Gtk.STOCK_FILE
folder_icon = Gtk.STOCK_DIRECTORY
for file_name in os.listdir(CURRENT_DIRECTORY):
modified_time = 0#os.path.getmtime(CURRENT_DIRECTORY+file_name)
size = 0#os.path.getsize(CURRENT_DIRECTORY+file_name)
if not file_name[0] == '.':
if os.path.isdir(os.path.join(CURRENT_DIRECTORY, file_name)):
list_store.append([
folder_icon,
file_name,
True,
modified_time,
size
])
else:
list_store.append([
file_icon,
file_name,
False,
modified_time,
size
])
The whole codebase is quite big so you can find it here: https://github.com/hell-abhi/File-Manager
The views are working fine individually, but with toggle they aren't working. How can I resolve it?
I read the solution that is proposed here: Gtk3 replace child widget with another widget
But in my case main_view
is calling two separate functions that retirns either listview
or iconview
.