我做了一个类,每当使用笔记本作为参数调用它时都会插入一个新选项卡。我喜欢做的是将适当的包含关联到这个新选项卡。我认为用这个小部件作为参数调用我的类 CreateTab 是个好主意。但是当我做:
>>> notebook.insert_page(treeview, hbox, 0)
我一直有这个错误:
Gtk-WARNING **: Can't set a parent on widget which has a parent
显然,我想做的是:每当我从主窗口单击一个按钮时,例如“鱼按钮”,它都会创建一个新选项卡,其中我的小部件显示“所有来自大海的鱼”。
这是我的“CreateTab”类:
class CreateTab():
def __init__(self, notebook, title):
self.notebook = notebook
self.pages = self.notebook.get_n_pages()
self.create_tab(title + str(self.pages))
self.notebook.set_current_page(self.pages)
def create_tab(self, title):
hbox = Gtk.HBox(False, 0)
label = Gtk.Label(title)
hbox.pack_start(label, True, True, 0)
pixbuf = Gtk.IconTheme.get_default().load_icon(Gtk.STOCK_CLOSE, 16,0)
image = Gtk.Image()
image.set_from_pixbuf(pixbuf)
btn = Gtk.Button()
btn.set_focus_on_click(False)
btn.add(image)
hbox.pack_start(btn, False, False, 0)
hbox.show_all()
#widget = Gtk.Label(title)
widget = Gtk.TreeView()
widget.show_all()
self.notebook.insert_page(widget, hbox,self.pages)
btn.connect('clicked', self.on_closetab_button_clicked, widget)
def on_closetab_button_clicked(self, sender, widget):
pagenum = self.notebook.page_num(widget)
self.notebook.remove_page(pagenum)
例如,我想关联的小部件:
类 CellRendererTextWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="CellRendererText Example")
self.set_default_size(200, 200)
self.liststore = Gtk.ListStore(str, str)
self.liststore.append(["Fedora", "http://fedoraproject.org/"])
self.liststore.append(["Slackware", "http://www.slackware.com/"])
self.liststore.append(["Sidux", "http://sidux.com/"])
treeview = Gtk.TreeView(model=self.liststore)
renderer_text = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
treeview.append_column(column_text)
renderer_editabletext = Gtk.CellRendererText()
renderer_editabletext.set_property("editable", True)
column_editabletext = Gtk.TreeViewColumn("Editable Text",
renderer_editabletext, text=1)
treeview.append_column(column_editabletext)
renderer_editabletext.connect("edited", self.text_edited)
self.add(treeview)
def text_edited(self, widget, path, text):
self.liststore[path][1] = text
由于我被困在这两天,我将不胜感激任何帮助或建议!