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()
非常感謝