我有一个Gtk.Treeview
显示项目的行。我想使用按钮 1(默认行为)选择行,并且在单击按钮 3(右)时,应该会出现一个上下文菜单。这是我得到的结果:
1 - 使用connect
连接到button-press-event
, 工作正常,但是,由于此处理程序在默认(树视图)处理程序之前调用,行选择尚未更改,读取选定行会给出先前选择的行。
2 -connect-after
用于连接到button-press-event
. 但是现在我的处理程序不再被调用......好像内部处理程序以return True
. 奇怪的是,双击确实会调用我的处理程序。
如何让 button-3 先更改选择,然后调用我的处理程序?
这是该程序的简短版本,取消注释标记的行以测试 1 或 2。
顺便说一句:我认为可能set_activate_on_single_click
会有所帮助 - 没有运气。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_keypress.py
#
# Copyright 2021 John Coppens <john@jcoppens.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GooCanvas', '2.0')
from gi.repository import Gtk, GooCanvas
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.set_default_size(400, 300)
store = Gtk.ListStore(str)
view = Gtk.TreeView(model = store)
view.connect('button-press-event', self.on_button_pressed) # Select one of these
# view.connect_after('button-press-event', self.on_button_pressed) # ....
renderer = Gtk.CellRendererText()
col = Gtk.TreeViewColumn('Column', renderer, text = 0)
view.append_column(col)
store.append(('Alpha', ))
store.append(('Beta', ))
store.append(('Gamma', ))
scroller = Gtk.ScrolledWindow()
scroller.add(view)
self.add(scroller)
self.show_all()
def on_button_pressed(self, view, event):
sel = view.get_selection()
store, selected = sel.get_selected()
item = store[selected]
print(event.button, item[0])
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))