2013-12-09 60 views
0

這些都是python的第一次嘗試,我想將2個小部件添加到gtk中。 我知道,看到你一次只能添加一個,並且已經知道如何去做。 有人能給我一個漏洞,能夠有一個「樹」和一個鼠標右鍵菜單嗎? 這是我的代碼:用Python添加2個小部件到gtk中

import gtk 

class treeNode(): 
    def __init__(self, father, name, link): 
     self.father = father 
     self.name = name 
     self.link = link 


class PyApp(gtk.Window): 
    def __init__(self): 
     super(PyApp, self).__init__() 

     self.set_size_request(700, 500) 
     self.set_position(gtk.WIN_POS_CENTER) 

     self.connect("destroy", gtk.main_quit) 
     self.set_title("Assignment1") 

     eventbox = gtk.EventBox() 

     tree = gtk.TreeView() 

     trying = gtk.TreeViewColumn() 
     trying.set_title("Get Busy") 

     cell = gtk.CellRendererText() 
     trying.pack_start(cell, True) 
     trying.add_attribute(cell, "text", 0) 

     treestore = gtk.TreeStore(str) 

     father = None 
     name = ["default"] 

     node = treestore.append(father, name) 

     node = treeNode(father, name, node) 
     lst = [node] 

     father = lst[0].link 
     name = ["cluster1"] 

     node = treestore.append(father, name) 

     node = treeNode(father, name, node) 
     lst.append(node) 

     father = lst[1].link 
     name = ["clusterA"] 

     node = treestore.append(father, name) 

     node = treeNode(father, name, node) 
     lst.append(node) 

     father = lst[0].link 
     name = ["cluster2"] 

     node = treestore.append(father, name) 

     node = treeNode(father, name, node) 
     lst.append(node) 

     father = lst[3].link 
     name = ["clusterA"] 

     node = treestore.append(father, name) 

     node = treeNode(father, name, node) 
     lst.append(node) 

     tree.append_column(trying) 
     tree.set_model(treestore) 

     self.add(tree) 
     self.show_all() 

     self.menu = gtk.Menu() 
     addMenu = gtk.MenuItem("Add") 
     renManu = gtk.MenuItem("Rename") 
     remMenu = gtk.MenuItem("Remove") 
     self.menu.append(addMenu) 
     self.menu.append(renManu) 
     self.menu.append(remMenu) 

     eventbox.connect("button-release-event", self.menu_display) 

     self.add(eventbox) 

     self.show_all() 



    def menu_display(self, widget, event): 
     if event.button == 3: 
      self.menu.popup(None, None, None, event.button, event.time, None) 
      self.menu.show_all() 


PyApp() 
gtk.main() 

非常感謝

回答

1

請儘量寫清楚的問題:「我要2只小部件添加到GTK」是沒有意義的。

我打算假設你想將多個小部件添加到一個Window(或其他Bin)中,是嗎?你不能這樣做,因爲一個Bin只能有一個孩子(就像運行時錯誤信息會告訴你的那樣):相反,你應該在窗口中添加一個合適的容器小部件 - 比如VBox - 然後將你的小部件添加到容器小部件。