【发布时间】:2017-11-28 09:19:51
【问题描述】:
【问题讨论】:
标签: python python-2.7 button pygtk
【问题讨论】:
标签: python python-2.7 button pygtk
-你真的想要一个按钮吗?按钮和标签的主要区别在于那些效果(标签不生成事件,但您可以通过将它们打包到 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 没有反应,底部的按钮仍然有。
【讨论】:
import gi。您还需要一个(或多或少)最新版本的 Gtk,它安装用于自省的表(可能就是这种情况)。最简单的测试方法 - 从上述程序的import gi 中执行 3 行。并考虑升级到 Python 3...它没有您想象的那么复杂!
pygtk。我在上面的程序中使用的方法也在那里可用。我没有安装 amd 更新 pygtk。只需使用import pygtk 而不是gi 代码/