I am trying to build a simple app that calculates the farenhit to celsius converter. Here is my code..

from gi.repository import Gtk, GObject

class DemoWindow(Gtk.Window):

  def __init__(self):
    Gtk.Window.__init__(self, title="Demo Example")
    self.set_size_request(300, 400)
    vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
    self.add(vbox)
    self.entry = Gtk.Entry()
    self.entry.set_text("Enter Farenhit")


    self.button1 = Gtk.Button(label="Calculate")
    self.button1.connect("clicked", self.on_button1_clicked)
    vbox.pack_start(self.entry, True, True, 0)
    vbox.pack_start(self.button1, True, True, 0)

  def on_button1_clicked(self, widget):
        ftemp=self.entry.get_text()
        return (ftemp-32) * 5 / 9


win = DemoWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Now the problem is i am getting the following error.

TypeError: unsupported operand type(s) for -: 'str' and 'int'

So How to resolve this error? All I am trying to return here is value taken from the entry box.

Any pointers?

Dani AI

Generated

The error happens because Gtk.Entry.get_text() returns a string, and you can’t do arithmetic on a plain string. was right to suggest converting the text to a numeric type, and correctly pointed out that using floats avoids integer-division pitfalls in older Python. Two extra points that aren’t in the thread yet: returning a value from a button callback does not display it (GTK ignores that return for a "clicked" handler), and many GUI apps don’t have a visible console — so using print() will often be invisible to the end user. Update a widget (or show a dialog) to make results visible.

A simple pattern to follow is: parse and validate the entry text, compute using floats, then write the formatted result into a Label (or into the Entry if you prefer). Example:

# create a result label and add it to the box
self.result = Gtk.Label("")
vbox.pack_start(self.result, False, False, 0)

# in the clicked handler
txt = self.entry.get_text().strip()
try:
    fahr = float(txt)
except ValueError:
    self.result.set_text("Please enter a valid number")
    return
celsius = (fahr - 32.0) * 5.0 / 9.0
self.result.set_text("{:.2f} °C".format(celsius))

Extra tips: trim whitespace and handle empty input; give friendly error text rather than letting exceptions bubble up; if you need exact decimal behavior use the decimal module; and if you launch the app from a terminal for debugging, print() works, but for user-facing output update a widget or pop up a message dialog.

Recommended Answers

All 3 Replies

ftemp is a string, you will need to convert it into an integer in order to calculate (ftemp - 32)*5 / 9
why not try ftemp = int(self.entry.get_text()) in your current code :).

Using
return (float(ftemp)-32) * 5 / 9
would also help you with the integer division problem with Python2

Code does compile but the output is not visible on the console :( . I wonder what could be wrong with the return statement?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.