python - 显示大型弹出菜单的正确方法是什么?

标签 python gtk gtk3 pygobject

一张图描绘一千个单词......:

enter image description here

在我的 Python 2.7 应用程序中,我有一个按钮,单击该按钮会弹出一个菜单。

在某些情况下,此列表大于屏幕尺寸。

  • 在 Ubuntu 12.04(使用 Gtk 3.4.2)中这是可以的,因为您会看到滚动箭头(如右图所示)。

  • 在 Ubuntu 12.10/13.04 和 Fedora 17(使用 Gtk 3.6)中,我得到相同的菜单,但没有滚动箭头,并且您不能使用鼠标向上或向下滚动。

奇怪的是,如果我再次单击该按钮 - 滚动箭头会重新出现。

所以它看起来像是某种大小分配问题 - 它不是在第一个弹出窗口中计算的,而是在随后的弹出窗口中计算的

因此我的问题

较新的 GTK 库显然已经发生了一些变化 - 现在显示大型弹出菜单以确保显示滚动箭头的正确方法是什么?

有什么提示我应该如何解决不同 GTK 版本之间的这种明显差异,以便我可以获得一致的“首次点击时显示箭头”?

下面是演示此问题的简单 python 测试程序。

我无法使用 GTKParasite 来诊断此问题,因为一旦您单击 GtkParasite 本身上的“检查”按钮,弹出窗口就会消失。

# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-

#!/usr/bin/env python

from gi.repository import Gtk

def popupclick(self, *args):
    popup.popup(None, None, None, None, 0,
            Gtk.get_current_event_time())

window = Gtk.Window()
window.connect('delete_event', Gtk.main_quit)
window.set_default_size(200,200)

first_item = None
popup = Gtk.Menu()

for i in range(100):
    label = 'Item %d' % i
    if not first_item:
        new_menu_item = Gtk.RadioMenuItem(label=label)
        first_item = new_menu_item
    else:
        new_menu_item = Gtk.RadioMenuItem.new_with_label_from_widget(
            group=first_item, label=label)

    new_menu_item.show()
    popup.append(new_menu_item)
    
button = Gtk.Button()
button.connect('clicked', popupclick)
mainbox = Gtk.Box()
mainbox.pack_start(button, True, True, 0)
scroller = Gtk.ScrolledWindow()
scroller.add_with_viewport(mainbox)
window.add(scroller)
    
window.show_all()

Gtk.main()

最佳答案

我浏览了一些文档,而不是使用 popup.append(new_menu_item) 你可以使用 popup.attach(new_menu_item, left, right, top, bottom) 将您的菜单项放在网格中,而不是一长行。

不过,看起来您最好还是打开一个带有可滚动列表的窗口!

关于python - 显示大型弹出菜单的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14423971/

相关文章:

python - 如何将 TF-IDF 矩阵转换为前 10 个单词的整体词典

python - sqlalchemy 对动态惰性关系的条件多重过滤器

无法在 GtkTreeView 可编辑行中使用键盘键入(但可以右键单击+粘贴)

user-interface - 用 GTK+2 或 GTK+3 学习 GUI 编程?

c++ - 设置 GtkButton 宽度

python - 如何一次从 Pandas 的所有列中删除逗号

python - Pandas 随时间绘制计数器的累计总和

c - GTK 扩展器一次一个

GTK+、C++、Windows、绑定(bind)到空地不起作用

python - 如何在python中正确显示和隐藏GTK窗口