如何从Gtk 3窗口动态添加/删除小部件 [英] How to dynamically add/remove widgets from Gtk 3 Window

查看:167
本文介绍了如何从Gtk 3窗口动态添加/删除小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将python3与Gtk3一起使用,我基本上需要从Gtk.Windowremove的一些小部件,当Gtk.Buttonclicked时将它们替换为其他小部件.我尝试将Gtk.ListBox es与Gtk.ListBoxRow s到目前为止,我可以从ListBox中删除所有Row,但是当我尝试将它们添加回去时,基本上什么都没发生.这是我的代码的一部分:

I am using python3 with Gtk3 and i need to basically remove some widgets from my Gtk.Window and replace them with other widgets when a Gtk.Button is clicked.I have tried using Gtk.ListBoxes with Gtk.ListBoxRows and so far i can remove all the Rows from the ListBox but when i try to add them back,essentially nothing happens.This is part of my code:

def left_view(self, box):
    listbox = box

    row0 = Gtk.ListBoxRow()
    row0.set_halign(Gtk.Align.START)
    listbox.add(row0)
    #Adding HorizontalBox to place widgets
    hbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.HORIZONTAL)
    row0.add(hbox)
    prod_name = Gtk.Label()
    stock_count = Gtk.Label()
    prod_name.set_text('Product Name')
    stock_count.set_text('Stock   ')
    self.prod_name_input = Gtk.Entry()
    self.stock_count_input = Gtk.Entry()

    #Packaging 101
    hbox.pack_start(prod_name, True, True, 0)
    hbox.pack_start(self.prod_name_input, True, True, 0)
    hbox.pack_start(stock_count, True, True, 0)
    hbox.pack_start(self.stock_count_input, True, True, 0)

该函数将被赋予Gtk.ListBox作为参数.到目前为止,当我尝试将remove()add()放入列表框时,我所拥有的是:

the function will be given a Gtk.ListBox as the argument.And what i have so far when trying to remove() and add() things to the listbox is:

def remover(self,widget):
        vbox = widget.get_parent()
        base = vbox.get_parent()
        children = base.get_children()
        listbox = children[1]
        for row in listbox.get_children():
            listbox.remove(row)
        with open('product_info.json', 'r') as d:
            data = d.read()
            j = json.loads(data)
            d.close()
        self.keys = list(j.keys())
        row0 = Gtk.ListBoxRow()
        listbox.add_child(row0)
        scrollwindow = Gtk.ScrolledWindow()
        scrollwindow.set_hexpand(True)
        scrollwindow.set_vexpand(True)
        row0.add_child(scrollwindow)
        tview = Gtk.TextView()
        scrollwindow.add(tview)
        textbuffer = tview.get_buffer()
        for item in range(0, len(self.keys)):
            textbuffer.insert_at_cursor('   %s\t\t %s \n' %(item, self.keys[item]))

顺便说一句,如果有一种简单的方法可以随时随地替换PyGObject中的小部件,请让我知道我在相关问题上看到的大多数答案,而这些答案是完全没用的

By the way,If there is a simpler way of just replacing widgets on the go in PyGObject,please do let me know coz most of the answers that i saw on the related questions where totally useless

推荐答案

我也对此感到困惑.事实证明,创建的窗口小部件必须用show()show_all()显式显示.就您而言:

I was baffled by this, too. It turned out the created widgets must be explicitly shown with show() or show_all(). In your case:

    row0.show_all()

作为最后一个陈述.

这篇关于如何从Gtk 3窗口动态添加/删除小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆