使用ToggleToolButton在ListView和IconView之间切换

时间:2018-04-15 18:20:47

标签: python user-interface gtk gtk3

我目前正在处理文件管理器,并且已经成功地单独实现了ListView和IconView。现在我想创建一个切换按钮,以便用户可以在这些视图之间切换。我想通过根据切换按钮的状态更改主视图来实现这一点:

if toggletoolbutton.get_active():
            main_view = create_icon_view()
        else:
            main_view = create_list_view()

为此,我创建了切换按钮的事件: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()

这是我的两家商店:

  1. icon_store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, bool, float, float)
  2. list_store = Gtk.ListStore(str, str, bool, float, float)
  3. 我正在使用:

    填充它们
    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
                    ])
    

    整个代码库非常大,所以你可以在这里找到它:https://github.com/hell-abhi/File-Manager

    视图单独运行良好,但通过切换它们不起作用。我该如何解决?

    我阅读了此处提出的解决方案:Gtk3 replace child widget with another widget

    但在我的情况下,main_view正在调用两个单独的函数,这些函数将重新计算listviewiconview

1 个答案:

答案 0 :(得分:0)

你必须做两件事:

  1. 替换GUI中的小部件
  2. 更新main_view变量
  3. 的值

    由于您未显示将main_view添加到窗口小部件层次结构的代码部分,因此我使用了here中的replace_widget函数:

    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