2 Replies - 2843 Views - Last Post: 18 March 2012 - 09:09 AM Rate Topic: -----

#1 Computer-Wiz  Icon User is offline

  • New D.I.C Head

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

Python Tkinter Displaying and Linking Text on Widgets with StringVar()

Posted 16 March 2012 - 07:31 AM

Hello, recently our class started GUI in Python with Tkinter. We have to make a temperature converter that will take the temperature from any entry (Celsius or Fahrenheit) and convert it (File Attached). We have to use the grid method to make our GUI. So this is how far i have got. I need some help linking the numbers from the entry box. I am confused on how i use methods and link up text in widgets. My teacher told me to use StringVar() but i cant figure out on how to use it properly. The program needs to recognise which entry box has been given the value(temp.) and then convert and display in the converted entry box.

Any Help would be appreciated!!
Thanks

Code:

from Tkinter import*



def click():
    c = 32
    celTemp.set(c)
    




#Createing MainFrame
root = Tk()
title = root.title("Temperature Converter")

mainframe = Frame(root)


#Widgets
titleLabel = Label(mainframe, text = "Celcius - Fahrenheit \nTemperature Converter", font = ("Arial", 24, "bold"), justify = CENTER)

celciusLabel = Label(mainframe, text = "Celcius Temperature", font = ("Arial", 12), bg = "green")
fahrenheitLabel = Label(mainframe, text = "Fahrenheit Temperature", font = ("Arial", 12), bg = "red")


celTemp = StringVar()

celciusEntry = Entry(mainframe, width = 15, bd =3, )

fahrenheitEntry = Entry(mainframe, width = 15, bd=3, textvariable = celTemp)

convertButton = Button(mainframe, text = "Convert", font = ("Arial", 16, "bold"), relief = RAISED, bd=5, command=click)


#Grid Widgets
mainframe.grid()

titleLabel.grid(row = 0, column =0, columnspan=4)

celciusLabel.grid(row = 1, column = 1, sticky=NW, pady = 50)
fahrenheitLabel.grid(row = 2, column = 1, sticky=NW)

celciusEntry.grid(row = 1, column = 2, sticky=NW, pady = 50, padx = 10)
fahrenheitEntry.grid(row = 2, column = 2, sticky = NW, padx = 10)

convertButton.grid(row = 3, column = 3, padx = 50, pady = 10, ipadx = 10, ipady = 10)


root.mainloop()

This post has been edited by modi123_1: 16 March 2012 - 07:35 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Python Tkinter Displaying and Linking Text on Widgets with StringVar()

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python Tkinter Displaying and Linking Text on Widgets with StringVar()

Posted 16 March 2012 - 08:36 AM

Create two variables, celTemp and farTemp as IntVar()s (or FloatVar()s), create two more called celOrg and farOrg. Use celOrg and farOrg to keep the original values of the fields; when the button is clicked, compare celTemp to celOrg and farTemp to farOrg. This way you will know which field they chose to edit, and thus which one you will need to update.

Another way would be to include two buttons, one for converting to Celsius, the other for converting to Fahrenheit.
Was This Post Helpful? 1
  • +
  • -

#3 Computer-Wiz  Icon User is offline

  • New D.I.C Head

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

Re: Python Tkinter Displaying and Linking Text on Widgets with StringVar()

Posted 18 March 2012 - 09:09 AM

hey i kind of understood your suggestion. This is my first time with GUI so i am confused on how i link methods with the widgets. I understand on how i can set the variables but how am i supposed to link the converting method to print the right temp. in the entry box. This is what i tried while experimenting; but i am not getting the right result.

from Tkinter import*
#Convert celcius temperature to fahrenheit
##def celToFah():
##    fahTemp = celTemp  *  9/5 + 32
    
#Convert fahrenhiet temperature to celcius
def fahToCel():
    pass
  

def click():
    temp = celTemp -  32 * 5/9  
    fahrenheitEntry.set(temp)

#Createing MainFrame
root = Tk()
title = root.title("Temperature Converter")

mainframe = Frame(root)


#Widgets
titleLabel = Label(mainframe, text = "Celcius - Fahrenheit \nTemperature Converter", font = ("Arial", 24, "bold"), justify = CENTER)

celciusLabel = Label(mainframe, text = "Celcius Temperature", font = ("Arial", 12), bg = "green")
fahrenheitLabel = Label(mainframe, text = "Fahrenheit Temperature", font = ("Arial", 12), bg = "red")


celTemp = IntVar()
celciusEntry = Entry(mainframe, width = 15, bd =3 )



fahrenheitEntry = Entry(mainframe, width = 15, bd=3, textvariable = celTemp)


convertButton = Button(mainframe, text = "Convert", font = ("Arial", 16, "bold"), relief = RAISED, bd=5, command=click)


#Grid Widgets
mainframe.grid()

titleLabel.grid(row = 0, column =0, columnspan=4)

celciusLabel.grid(row = 1, column = 1, sticky=NW, pady = 50)
fahrenheitLabel.grid(row = 2, column = 1, sticky=NW)

celciusEntry.grid(row = 1, column = 2, sticky=NW, pady = 50, padx = 10)
fahrenheitEntry.grid(row = 2, column = 2, sticky = NW, padx = 10)

convertButton.grid(row = 3, column = 3, padx = 50, pady = 10, ipadx = 10, ipady = 10)


root.mainloop()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1