【问题标题】:How to remove hover/clicked effect in pygtk?如何删除 pygtk 中的悬停/单击效果?
【发布时间】:2017-11-28 09:19:51
【问题描述】:

我想删除或关闭此悬停并单击 pygtk 中按钮的效果。你有什么想法?我在这里找不到任何有用的东西gtk.Button

我说的是这个:

Normal

Hover

Clicked

【问题讨论】:

    标签: python python-2.7 button pygtk


    【解决方案1】:

    -你真的想要一个按钮吗?按钮和标签的主要区别在于那些效果(标签不生成事件,但您可以通过将它们打包到 Gtk.EventBox 中来实现)

    -这是首先禁用导致效果的事件的方法:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    #
    #  test_inert_button.py
    #
    #  Copyright 2017 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')
    from gi.repository import Gtk
    
    class MainWindow(Gtk.Window):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.connect("destroy", lambda x: Gtk.main_quit())
            self.set_size_request(400, 300)
    
            btn1 = Gtk.Button("One - inert button")
            btn2 = Gtk.Button("Two - active button")
    
            btn1.connect("enter-notify-event", self.on_leave)
            btn1.connect("button-press-event", self.on_leave)
    
            vbox = Gtk.VBox()
            vbox.pack_start(btn1, False, False, 0)
            vbox.pack_start(btn2, False, False, 0)
    
            self.add(vbox)
            self.show_all()
    
        def on_leave(self, btn, event):
            return True
    
        def run(self):
            Gtk.main()
    
    
    def main(args):
        mainwdw = MainWindow()
        mainwdw.run()
    
        return 0
    
    if __name__ == '__main__':
        import sys
        sys.exit(main(sys.argv))
    

    on_leave 方法的return True 告诉默认处理程序事件已被处理,因此它不会产生效果。顶部的按钮One 没有反应,底部的按钮仍然有。

    【讨论】:

    • 如果您为自省安装了必要的模块,它应该可以工作。尝试在 python 中import gi。您还需要一个(或多或少)最新版本的 Gtk,它安装用于自省的表(可能就是这种情况)。最简单的测试方法 - 从上述程序的import gi 中执行 3 行。并考虑升级到 Python 3...它没有您想象的那么复杂!
    • 是的,但我必须使用旧的 pygtk 和 7 年的基于 arch 的系统,我无法安装额外的模块,所以......你看。
    • 刚刚在我安装的 Python 2.7 下运行了程序,它没有任何变化。
    • 顺便说一句,我怀疑这也适用于pygtk。我在上面的程序中使用的方法也在那里可用。我没有安装 amd 更新 pygtk。只需使用import pygtk 而不是gi 代码/
    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2013-10-03
    • 2018-11-08
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多