• We just launched and are currently in beta. Join us as we build and grow the community.

Python: Simple Temperature Conversion

DarkGamers11

Meme Curator
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
172
Likes
196
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Python: Simple Temperature Conversion Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now do the coding.

Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy the code that I provided below and paste it inside the IDLE text editor
  1. from

    tkinter import

    *

Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. root =

    Tk(

    )
  2. root.title

    (

    "Sourcecodester"

    )
  3. width =

    700
  4. height =

    400
  5. screen_width =

    root.winfo_screenwidth

    (

    )
  6. screen_height =

    root.winfo_screenheight

    (

    )
  7. x =

    (

    screen_width/2

    )

    - (

    width/2

    )
  8. y =

    (

    screen_height/2

    )

    - (

    height/2

    )
  9. root.geometry

    (

    "%dx%d+%d+%d"

    % (

    width,

    height,

    x,

    y)

    )
  10. root.resizable

    (

    0

    ,

    0

    )
  11. root.config

    (

    bg=

    "#ff9999"

    )

Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. #=======================VARIABLES======================
  2. METER =

    StringVar(

    )
  3. METER1 =

    StringVar(

    )
  4. CENTIMETER =

    StringVar(

    )
  5. CENTIMETER1 =

    StringVar(

    )
  6. CELCIUS =

    StringVar(

    )
  7. CELCIUS1 =

    StringVar(

    )
  8. FAHRENHEIT =

    StringVar(

    )
  9. FAHRENHEIT1 =

    StringVar(

    )

  10. #=======================FRAME==========================
  11. Top =

    Frame(

    root,

    width=

    700

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  12. Top.pack

    (

    side=

    TOP)
  13. TopTitle =

    Frame(

    root,

    width=

    700

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  14. TopTitle.pack

    (

    side=

    TOP,

    pady=

    10

    )
  15. TopForm =

    Frame(

    root,

    width=

    700

    ,

    bg=

    "#ff9999"

    )
  16. TopForm.pack

    (

    side=

    TOP)
  17. TopLeft =

    Frame(

    TopForm,

    width=

    350

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  18. TopLeft.pack

    (

    side=

    LEFT,

    padx=

    10

    )
  19. TopRight =

    Frame(

    TopForm,

    width=

    350

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  20. TopRight.pack

    (

    side=

    RIGHT,

    padx=

    10

    )
  21. BottomTitle =

    Frame(

    root,

    width=

    700

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  22. BottomTitle.pack

    (

    side=

    TOP,

    pady=

    10

    )
  23. BottomForm =

    Frame(

    root,

    width=

    700

    ,

    bg=

    "#ff9999"

    )
  24. BottomForm.pack

    (

    side=

    TOP)
  25. BottomLeft =

    Frame(

    BottomForm,

    width=

    250

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  26. BottomLeft.pack

    (

    side=

    LEFT)
  27. BottomRight =

    Frame(

    BottomForm,

    width=

    250

    ,

    bd=

    1

    ,

    relief=

    SOLID)
  28. BottomRight.pack

    (

    side=

    RIGHT,

    padx=

    10

    )


  29. #=======================LABEL WIDGET===================
  30. lbl_title =

    Label(

    Top,

    text=

    "Python: Simple Temperature Conversion"

    ,

    width=

    700

    ,

    font=

    (

    'arial'

    ,

    18

    )

    )
  31. lbl_title.pack

    (

    fill=

    X)
  32. lbl_TopTitle =

    Label(

    TopTitle,

    text=

    "Metric"

    ,

    width=

    700

    ,

    font=

    (

    'arial'

    ,

    12

    )

    ,

    bg=

    "#ccccff"

    )
  33. lbl_TopTitle.pack

    (

    fill=

    X)
  34. lbl_BottomTitle =

    Label(

    BottomTitle,

    text=

    "Temperature"

    ,

    width=

    700

    ,

    font=

    (

    'arial'

    ,

    12

    )

    ,

    bg=

    "#ccccff"

    )
  35. lbl_BottomTitle.pack

    (

    fill=

    X)
  36. lbl1 =

    Label(

    TopLeft,

    text=

    "Meter To Centimeter"

    ,

    font=

    (

    'arial'

    ,

    12

    )

    )
  37. lbl1.grid

    (

    row=

    0

    ,

    columnspan=

    5

    ,

    column=

    0

    )
  38. lbl2 =

    Label(

    TopRight,

    text=

    "Centimeter To Meter"

    ,

    font=

    (

    'arial'

    ,

    12

    )

    )
  39. lbl2.grid

    (

    row=

    0

    ,

    columnspan=

    5

    ,

    column=

    0

    )
  40. lbl3 =

    Label(

    BottomLeft,

    text=

    "Celcius To Fahrenheit"

    ,

    font=

    (

    'arial'

    ,

    12

    )

    )
  41. lbl3.grid

    (

    row=

    0

    ,

    columnspan=

    5

    ,

    column=

    0

    )
  42. lbl4 =

    Label(

    BottomRight,

    text=

    "Farenheit To Celcius"

    ,

    font=

    (

    'arial'

    ,

    12

    )

    )
  43. lbl4.grid

    (

    row=

    0

    ,

    columnspan=

    5

    ,

    column=

    0

    )
  44. lbl_meter =

    Label(

    TopLeft,

    text=

    "Meter"

    ,

    font=

    (

    12

    )

    )
  45. lbl_meter.grid

    (

    row=

    1

    ,

    padx=

    5

    ,

    pady=

    10

    )
  46. lbl_meter1 =

    Label(

    TopRight,

    text=

    "Meter"

    ,

    font=

    (

    12

    )

    )
  47. lbl_meter1.grid

    (

    row=

    1

    ,

    column=

    3

    ,

    padx=

    5

    ,

    pady=

    10

    )
  48. lbl_to =

    Label(

    TopLeft,

    text=

    "-"

    )
  49. lbl_to.grid

    (

    row=

    1

    ,

    column=

    2

    )
  50. lbl_to1 =

    Label(

    TopRight,

    text=

    "-"

    )
  51. lbl_to1.grid

    (

    row=

    1

    ,

    column=

    2

    )
  52. lbl_centimeter =

    Label(

    TopLeft,

    text=

    "Centimeter"

    ,

    font=

    (

    12

    )

    )
  53. lbl_centimeter.grid

    (

    row=

    1

    ,

    column=

    3

    )
  54. lbl_centimeter1 =

    Label(

    TopRight,

    text=

    "Centimeter"

    ,

    font=

    (

    12

    )

    )
  55. lbl_centimeter1.grid

    (

    row=

    1

    )
  56. lbl_celcius =

    Label(

    BottomLeft,

    text=

    "Celcius"

    ,

    font=

    (

    12

    )

    )
  57. lbl_celcius.grid

    (

    row=

    1

    ,

    padx=

    5

    ,

    pady=

    10

    )
  58. lbl_celcius1 =

    Label(

    BottomRight,

    text=

    "Celcius"

    ,

    font=

    (

    12

    )

    )
  59. lbl_celcius1.grid

    (

    row=

    1

    ,

    column=

    3

    )
  60. lbl_to2 =

    Label(

    BottomLeft,

    text=

    "-"

    )
  61. lbl_to2.grid

    (

    row=

    1

    ,

    column=

    2

    )
  62. lbl_to3 =

    Label(

    BottomRight,

    text=

    "-"

    )
  63. lbl_to3.grid

    (

    row=

    1

    ,

    column=

    2

    )
  64. lbl_fahrenheit =

    Label(

    BottomLeft,

    text=

    "Fahrenheit"

    ,

    font=

    (

    12

    )

    )
  65. lbl_fahrenheit.grid

    (

    row=

    1

    ,

    column=

    3

    )
  66. lbl_fahrenheit1 =

    Label(

    BottomRight,

    text=

    "Fahrenheit"

    ,

    font=

    (

    12

    )

    )
  67. lbl_fahrenheit1.grid

    (

    row=

    1

    ,

    padx=

    5

    ,

    pady=

    10

    )

  68. #=======================ENTRY WIDGET===================
  69. meter1 =

    Entry(

    TopLeft,

    textvariable=

    METER,

    width=

    12

    )
  70. meter1.grid

    (

    row=

    1

    ,

    column=

    1

    )
  71. meter2 =

    Entry(

    TopRight,

    textvariable=

    METER1,

    width=

    12

    ,

    state=

    DISABLED)
  72. meter2.grid

    (

    row=

    1

    ,

    column=

    4

    ,

    padx=

    5

    )
  73. centimeter1 =

    Entry(

    TopLeft,

    textvariable=

    CENTIMETER,

    width=

    12

    ,

    state=

    DISABLED)
  74. centimeter1.grid

    (

    row=

    1

    ,

    column=

    4

    ,

    padx=

    5

    )
  75. centimeter2 =

    Entry(

    TopRight,

    textvariable=

    CENTIMETER1,

    width=

    12

    )
  76. centimeter2.grid

    (

    row=

    1

    ,

    column=

    1

    )
  77. celcius1 =

    Entry(

    BottomLeft,

    textvariable=

    CELCIUS,

    width=

    12

    )
  78. celcius1.grid

    (

    row=

    1

    ,

    column=

    1

    )
  79. celcius2 =

    Entry(

    BottomRight,

    textvariable=

    CELCIUS1,

    width=

    12

    ,

    state=

    DISABLED)
  80. celcius2.grid

    (

    row=

    1

    ,

    column=

    4

    ,

    padx=

    5

    )
  81. fahrenheit1 =

    Entry(

    BottomLeft,

    textvariable=

    FAHRENHEIT,

    width=

    12

    ,

    state=

    DISABLED)
  82. fahrenheit1.grid

    (

    row=

    1

    ,

    column=

    4

    ,

    padx=

    5

    )
  83. fahrenheit2 =

    Entry(

    BottomRight,

    textvariable=

    FAHRENHEIT1,

    width=

    12

    )
  84. fahrenheit2.grid

    (

    row=

    1

    ,

    column=

    1

    )

  85. #=======================BUTTON WIDGET==================
  86. btn1 =

    Button(

    TopLeft,

    text=

    "Convert"

    ,

    width=

    20

    ,

    bg=

    "#006dcc"

    ,

    command=

    MeterToCenti)
  87. btn1.grid

    (

    row=

    2

    ,

    columnspan=

    5

    ,

    column=

    0

    ,

    pady=

    10

    )
  88. btn2 =

    Button(

    TopRight,

    text=

    "Convert"

    ,

    width=

    20

    ,

    bg=

    "#006dcc"

    ,

    command=

    CentiToMeter)
  89. btn2.grid

    (

    row=

    2

    ,

    columnspan=

    5

    ,

    column=

    0

    ,

    pady=

    10

    )
  90. btn3 =

    Button(

    BottomLeft,

    text=

    "Convert"

    ,

    width=

    20

    ,

    bg=

    "#006dcc"

    ,

    command=

    CelciusToFahren)
  91. btn3.grid

    (

    row=

    2

    ,

    columnspan=

    5

    ,

    column=

    0

    ,

    pady=

    10

    )
  92. btn4 =

    Button(

    BottomRight,

    text=

    "Convert"

    ,

    width=

    20

    ,

    bg=

    "#006dcc"

    ,

    command=

    FahrenToCelcius)
  93. btn4.grid

    (

    row=

    2

    ,

    columnspan=

    5

    ,

    column=

    0

    ,

    pady=

    10

    )

Creating the Main Function
This is the main function where the conversion is being processed. To do that just simply copy the code below then paste it inside the IDLE text editor
  1. #=======================METHODS========================
  2. def

    MeterToCenti(

    )

    :
  3. CENTIMETER.set

    (

    ""

    )
  4. if

    METER.get

    (

    )

    !=

    ""

    :
  5. CENTIMETER.set

    (

    float

    (

    METER.get

    (

    )

    )

    * 100

    )

  6. def

    CentiToMeter(

    )

    :
  7. METER1.set

    (

    ""

    )
  8. if

    CENTIMETER1.get

    (

    )

    !=

    ""

    :
  9. METER1.set

    (

    float

    (

    CENTIMETER1.get

    (

    )

    )

    / 100

    )

  10. def

    CelciusToFahren(

    )

    :
  11. FAHRENHEIT.set

    (

    ""

    )
  12. if

    CELCIUS.get

    (

    )

    !=

    ""

    :
  13. FAHRENHEIT.set

    (

    (

    float

    (

    CELCIUS.get

    (

    )

    )

    *(

    9

    /5

    )

    )

    + 32

    )

  14. def

    FahrenToCelcius(

    )

    :
  15. CELCIUS1.set

    (

    ""

    )
  16. if

    FAHRENHEIT1.get

    (

    )

    !=

    ""

    :
  17. CELCIUS1.set

    (

    (

    float

    (

    FAHRENHEIT1.get

    (

    )

    )

    - 32

    )

    *(

    5

    /9

    )

    )


Initializing the Application
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #=======================INITIALIZATION=================
  2. if

    __name__ ==

    '__main__'

    :
  3. root.mainloop

    (

    )

There you have it we just created a Simple Temperature Conversion Using Python. I hope that this simple tutorial help you expand your idea about Python programming. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top