2016-09-19 142 views
1

我有一个Gtk.Notebook有一个自定义的弹出菜单,当用户右击任何页面按钮时显示。如何知道用户点击了Gtk.Notebook中的哪个页面?

如何知道用户点击了哪个笔记本页面?我想在我的菜单中添加一个动作,使其成为当前页面。

notebook.button_press_event.connect((wid,evt) => { 
    if (evt.button==3) { 
     // which page button did the user click on? 
     notebook.set_current_page(«clicked no tab»); 
     // ... make it the current page 
    } 
} 

我试图通过位置找到标签:

int numtab = notebook.get_tab_at_pos((int)evt.x, (int)evt.y); 

但似乎没有成为一个get_tab_at_pos或类似的方法。

+0

所以你要调用'gtk_notebook_set_current_page()'在用户右键点击一个标签?这就是通常使用的鼠标左键,为什么还要使用右键? –

+0

,因为我显示的菜单会在此选项卡上执行操作,而鼠标左键对其他操作很有用,感谢您的介入 – bul

+0

鼠标左键:切换选项卡例如 – bul

回答

0

一种解决方案是使用Gtk.EventBox as suggested here(PHP代码):

$window = new GtkWindow(); 
$window->set_size_request(400, 240); 
$window->connect_simple('destroy', array('Gtk','main_quit')); 
$window->add($vbox = new GtkVBox()); 

// setup notebook 
$notebook = new GtkNotebook(); // note 1 
$vbox->pack_start($notebook); 

// add two tabs of GtkLabel 
add_new_tab($notebook, new GtkLabel('Notebook 1'), 'Label #1'); 
add_new_tab($notebook, new GtkLabel('Notebook 2'), 'Label #2'); 

// add a thrid tab of GtkTextView 
$buffer = new GtkTextBuffer(); 
$view = new GtkTextView(); 
$view->set_buffer($buffer); 
$view->set_wrap_mode(Gtk::WRAP_WORD); 
add_new_tab($notebook, $view, 'TextView'); 

$window->show_all(); 
Gtk::main(); 

// add new tab 
function add_new_tab($notebook, $widget, $tab_label) { 
    $eventbox = new GtkEventBox(); 
    $label = new GtkLabel($tab_label); 
    $eventbox->add($label); // note 2 
    $label->show(); // note 3 
    $eventbox->connect('button-press-event', 'on_tab', $tab_label); // note 4 
    $notebook->append_page($widget, $eventbox); // note 5 
} 

// function that is called when user click on tab 
function on_tab($widget, $event, $tab_label) { // note 6 
    echo "tab clicked = $tab_label\n"; 
} 
+0

试图理解,适应,我告诉你(也许不是马上)谢谢 – bul

+0

它完美的作品。 它稍微复杂一些, 标签的标签=图片+标签, 和循环在标签找到合适的。 再次感谢。 – bul

相关问题