Question

I'm building a GTK application with Python that makes an equation that always equals 42. I'm launching it from a linux command line. It says my function isn't callable or something. I think it's callable and I really don't know what this means, sorry for posting such a nooby question. Here's the code:

#!/usr/bin/env python

import random
import gtk

def makePlus():
    num2 = random.randint(0,41)
    num = 42 - num2
    return str(num) + " + " + str(num2)

def makeMinus():
    num2 = random.randint(0,41)
    num = 42 + num2
    return str(num) + " - " + str(num2)

def makeTimes():
    num2 = random.randint(0,41)
    num2 = float(num2)
    num = 42.0 / num2
    return str(num) + " x " + str(num2)

def makeDivideBy():
    num2 = random.randint(0,41) 
    num = 42 * num2
    return str(num) + " / " + str(num2)

def setResultAs(operation):
    if operation == "plus":
        result = makePlus()
    if operation == "minus":
        result = makeMinus()
    if operation == "times":
        result = makeTimes()
    if operation == "divide":
        result = makeDivideBy()
    textbox.set_text(result)

win = gtk.Window()
win.set_title("OSFT Equation Generator")
win.connect('delete-event', gtk.main_quit)

mainbox = gtk.VBox()
win.add(mainbox)

textbox = gtk.Entry()
mainbox.pack_start(textbox)

btnbox = gtk.HBox()
mainbox.pack_start(btnbox)

plusBtn = gtk.Button("Plus")
btnbox.pack_start(plusBtn)
plusBtn.connect("clicked", setResultAs("plus"))

minusBtn = gtk.Button("Minus")
btnbox.pack_start(minusBtn)
minusBtn.connect("clicked", setResultAs("minus"))

timesBtn = gtk.Button("Times")
btnbox.pack_start(timesBtn)
timesBtn.connect("clicked", setResultAs("times"))

divideBtn = gtk.Button("Divide by")
btnbox.pack_start(plusBtn)
divideBtn.connect("clicked", setResultAs("divide"))

win.show_all()
gtk.main()

And this is the error i get:

Traceback (most recent call last):
  File "./osftgen.py", line 53, in <module>
    plusBtn.connect("clicked", setResultAs("plus"))
TypeError: second argument must be callable

Please help.

Was it helpful?

Solution 2

def clickedPlus(widget):
    setResultAs("plus")

plusBtn.connect("clicked", clickedPlus)

You when connecting events to action, you need to pass a string event name and then a callable function, meaning "when the event named 'clicked' happens to the plusBtn, run the clickedPlus function.

OTHER TIPS

The code calls the setResultAs(..) and use the return value of the function as the callback (handler).

Use following form (gobject.GObject.connect):

object.connnect("signla_name", handler, arg1, arg2, ...)

For example, replace following line

plusBtn.connect("clicked", setResultAs("plus"))

with:

plusBtn.connect("clicked", setResultAs, "plus")

UPDATE

In addition to the above, the callback function setResultAs should receive widget (the widget that emit the signal) as the first parameter.

Replace following line:

def setResultAs(operation):

with:

def setResultAs(widget, operation):

And there's a typo:

divideBtn = gtk.Button("Divide by")
btnbox.pack_start(plusBtn) # <-------- `plusBtn` should be `divideBtn`
divideBtn.connect("clicked", setResultAs("divide"))

Callable is Python's abstract way of saying "is it some kind of function?"

You haven't passed a function to connect; you've passed None (because setResultAs doesn't return a value). Have something there that gtk can call back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top