gpt4 book ai didi

python - 从pygtk中的button_press_event发送 TreeView 信息

转载 作者:行者123 更新时间:2023-12-01 06:11:25 24 4
gpt4 key购买 nike

我正在编写一个使用 TreeView 的 pygtk 应用程序,它与 button_press_event 有连接。我不知道如何将有关 TreeView 的信息(特别是单击的行)传递给 gtk.Menu 或其他方法。如果我使用 row-activated 信号,它会传入行和列信息作为参数,但 button_press_event 不会发生这种情况。这是有问题的代码:

    self.liststore = gtk.ListStore(str,int, int, int,str, 'gboolean')
self.treeview = gtk.TreeView(self.liststore)

self.treeview.connect("button_press_event",self.serverListEvent)
self.treeview.set_search_column(0)
self.draw_columns(self.treeview)

self.blackmenu = gtk.Menu()
self.bitem = gtk.MenuItem("Blacklist server")
self.blackmenu.append(self.bitem)
self.bitem.connect("activate",self.blacklistServer)
self.bitem.show()

def serverListEvent(self,treeview,event):
x = int(event.x)
y = int(event.y)
time = event.time
model = treeview.get_model()

pthinfo = treeview.get_path_at_pos(x, y)
if pthinfo is not None:
path, col, cellx, celly = pthinfo
# Error here for the model with the column
print 'url clicked '+model[col][0]
treeview.grab_focus()
treeview.set_cursor( path, col, 0)
# Popup blacklist menu on right click
if event.button == 3:
self.blackmenu.popup( None, None, None, event.button, time)
# Join game on double click
elif event.type == gtk.gdk._2BUTTON_PRESS:
self.joinGame(treeview,model[col][0])
return True

然后,我需要将信息从单击的行传递到 self.joinGameself.blacklistServer 方法,但也不知道如何执行此操作。

最佳答案

我记得那个有点棘手。我set up my treeview's gtk.TreeSelection无论什么mode of selection首选。然后这个:

def get_selected_ids(self):
ids = []
store, paths = self.get_selection().get_selected_rows()

# self.adapter is a mapping class from my data-model to a model column index.
# If you know the column index from your gtk.ListStore, you don't need it.
colindex = self.adapter.get_column_modelindex("id")

for path in paths:
treeiter = store.get_iter(path)
# Pull value from liststore
val = store.get_value(
treeiter,
colindex
)
ids.append(val)
return ids

编辑:

我从未使用过 gtk.Menu 和 gtk.MenuItem,所以我不知道您已经将菜单连接到回调(blacklistServer)。这尚未经过测试,但应该可以给您一个想法。

def blacklistServer(self, menuitem, *ignore):
# you have to modify *get_selected_ids* so it returns the values you need for
# blacklisting.
values = self.get_selected_ids()
blacklist = set(values)
self.saveBlacklist(blacklist)

关于python - 从pygtk中的button_press_event发送 TreeView 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5691976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com