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?

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.