0

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:

  1. icon_store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, bool, float, float)
  2. 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.

Abhishek Keshri
  • 3,074
  • 14
  • 31
  • Is the code that populates your Liststores relevant to the question in any way? You just want to replace one widget with another widget, no? – Aran-Fey Apr 15 '18 at 18:27
  • Possible duplicate of [Gtk3 replace child widget with another widget](https://stackoverflow.com/questions/27343166/gtk3-replace-child-widget-with-another-widget) – Aran-Fey Apr 15 '18 at 18:29
  • it is used in `click_toggle_button`, so I provided the reference if someone needed it. – Abhishek Keshri Apr 15 '18 at 18:30
  • @Aran-Fey thanks for the link, I will check it and update the question if need be, else will delete it. – Abhishek Keshri Apr 15 '18 at 18:33
  • @Aran-Fey how can I use this function to assign the view to main_view during the `click_toggle_button`? – Abhishek Keshri Apr 15 '18 at 18:42
  • You don't. You use the function to replace the widget in the gui. You use an assignment like `main_view = create_icon_view()` to update the value of your variable. Those are two different things you have to do. – Aran-Fey Apr 15 '18 at 19:11
  • so what will be old and new in my case? the iconview and treeview doesn't exist by its own. they are created using different functions – Abhishek Keshri Apr 15 '18 at 19:13

1 Answers1

0

You have to do two things:

  1. Replace the widget in the GUI
  2. Update the value of the main_view variable

Since you haven't show the part of the code where the main_view is added to the widget hierarchy, I made use of the replace_widget function from here:

def click_toggle_button(widget):
    global main_view
    if(toggletoolbutton.get_active()):
        populate_icon_store()
    else:
        populate_list_store()

    if toggletoolbutton.get_active():
        new_main_view = create_icon_view()
    else:
        new_main_view = create_list_view()
    replace_widget(main_view, new_main_view)
    main_view = new_main_view
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149