pygtk - B1() 仅接受 1 个参数(给定 2 个)

pygtk - B1() takes only 1 argument (2 given)

我正在使用 PyGTK,当我尝试将我的 GUI 信号(这只是使用 Glade 在 XML 中编写的一个按钮)绑定到函数时,我收到以下错误:

TypeError: B1() takes exactly 1 argument (2 given)

这是我的 Python 代码:

#!/usr/bin/python2

import pygtk
pygtk.require('2.0')
from gi.repository import Gtk as gtk

class Main():
    def __init__(self):
        self.builder = gtk.Builder()
        self.builder.add_from_file('/home/jatin/button.glade')
        self.MainWindow = self.builder.get_object('GW1')
        if self.MainWindow:
            self.MainWindow.connect("destroy", gtk.main_quit)

        dic = {
            'on_B1_clicked' : self.B1,
            'on_B2_clicked' : self.B2,
            'on_B3_clicked' : self.B3
        }
        self.builder.connect_signals(dic)
    def B1(self):
        return('ham')

    def B2(self):
        return('ham')

    def B3(self):
        return('ham')


M = Main()
gtk.main()

这是我的 Glade 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="GW1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox" id="GB1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkButton" id="B1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_B1_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="B2">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="B3">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

有趣的是,如果我改变传入的参数数量,错误信息会变成这样:

参数为零:

TypeError: B1() takes no arguments (2 given)

使用 2 个参数时,我没有收到任何错误,但该函数没有执行预期的操作。并且有 3 个或更多参数:

TypeError: B1() takes 3 arguments (2 given)

每个信号都有特定的参数传递给连接的信号处理程序。查看文档以了解它们是什么。例如,您可以看到 Gtk.Button's clicked signal 接收按钮本身作为参数。

您的信号处理程序的参数数量必须与这些参数相匹配,如果它是一个方法,还包括 self。因此,B1 的正确定义是:

def B1(self, button):

在某些情况下,您可能真的不关心传递给信号处理程序的值。在这种情况下,您可以执行以下操作:

def B1(self, *args):

这在文档中有解释 here