1 Replies - 566 Views - Last Post: 15 June 2012 - 07:10 PM Rate Topic: ***** 1 Votes

#1 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Tkinter Widgets

Posted 13 June 2012 - 05:12 PM

Hello again, this time i have been doing fairly good with Tkinter and graphics but i have come across this problem that i just cant get around.

SO i had to make a temp converter. I have made the whole application and i can convert from cel to fah but i dont understand why i cant convert from fah to cel although i have written the code to do so. i am not sure is it the string var or is it just the if statements i have tried changing alot but i need help now.


from Tkinter import*

def convert():
    fahTemp = fahTempVar.get()
    celTemp = celTempVar.get()
    
    if celTemp == 0:
        fahToCel = ((fahTemp - 32) * (5/9))

        celTempVar.set(fahToCel)
        
    if fahTemp == 0:
        celToFah = (celTemp *  9/5 + 32)
   
        fahTempVar.set(celToFah)
    
def reset():
    top = Toplevel(padx=50, pady=50)
    top.grid()
    message = Label(top, text = "Reset Complete")
    button = Button(top, text="OK", command=top.destroy)
    
    message.grid(row = 0, padx = 5, pady = 5)
    button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5)
    
    fahTempVar.set(0)
    celTempVar.set(0)

###MAIN###
root = Tk()
root.title("Temperature Converter")
mainframe = Frame(root)
mainframe.grid()

celTempVar = IntVar()
##celTempVar.set()
fahTempVar = IntVar()
##fahTempVar.set()

titleLabel = Label (mainframe, text = "Celcius/Fahrenheit Temperature", font = ("Arial", 20, "bold"), justify = CENTER)
titleLabel.grid(row = 1, column = 1, columnspan = 3, pady = 10, padx = 20)

celLabel = Label (mainframe, text = "Celcius: ", font = ("Arial", 16), fg = "red")
celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW)

fahLabel = Label (mainframe, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue")
fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW)

celEntry = Entry (mainframe, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 )

fahEntry = Entry (mainframe, width = 10, bd = 5, textvariable = fahTempVar)
fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )

convertButton =Button (mainframe, text = "Convert", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = convert)
convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)

resetButton = Button (mainframe, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = reset)
resetButton.grid(row = 5, column = 1,ipady = 8, ipadx = 12, pady = 5, sticky = NW)

root.mainloop()




Is This A Good Question/Topic? 0
  • +

Replies To: Tkinter Widgets

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Tkinter Widgets

Posted 15 June 2012 - 07:10 PM

in Python2, 5/7 evaluates to 0. you want to change
fahToCel = ((fahTemp - 32) * (5/9))
to
fahToCel = ((fahTemp - 32) * (5.0/9))

Don't forget, if this post or any other helps hit the Posted Image.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1