-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSC15 - Lab #12.py
77 lines (61 loc) · 2.67 KB
/
CSC15 - Lab #12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import tkinter
class TempConverterGUI:
def __init__(self):
# Create the main window.
self.main_window = tkinter.Tk()
# Create three frames to group widgets.
self.top_frame = tkinter.Frame()
self.mid_frame = tkinter.Frame()
self.bottom_frame = tkinter.Frame()
# Create the widgets for the top frame.
self.prompt_label = tkinter.Label(self.top_frame,
text= 'Enter a temperature in Celsius:')
self.temp_entry = tkinter.Entry(self.top_frame, width=10)
# Pack the top frame’s widgets.
self.prompt_label.pack(side='left')
self.temp_entry.pack(side='left')
# Create the widgets for the middle frame.
self.descr_label = tkinter.Label(self.mid_frame,
text= 'Converted to Fahrenheit:')
# We need a StringVar object to associate with
# an output label. Use the object’s set method
# to store a string of blank characters.
self.value = tkinter.StringVar()
# Create a label and associate it with the
# StringVar object. Any value stored in the
# StringVar object will automatically be displayed
# in the label.
self.miles_label = tkinter.Label(self.mid_frame,
textvariable=self.value)
# Pack the middle frame’s widgets.
self.descr_label.pack(side='left')
self.miles_label.pack(side='left')
# Create the button widgets for the bottom frame.
self.calc_button = tkinter.Button(self.bottom_frame,
text='Convert',command=self.convert)
self.quit_button = tkinter.Button(self.bottom_frame,
text='Quit',
command=self.main_window.destroy)
# Pack the buttons.
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')
# Pack the frames.
self.top_frame.pack()
self.mid_frame.pack()
self.bottom_frame.pack()
# Enter the tkinter main loop.
tkinter.mainloop()
# The convert method is a callback function for
# the Calculate button.
def convert(self):
# Get the value entered by the user into the
# temp_entry widget.
temp = float(self.temp_entry.get())
# Convert kilometers to miles.
fahrenheit = temp * (9/5) + 32
# Convert miles to a string and store it
# in the StringVar object. This will automatically
# update the miles_label widget.
self.value.set(fahrenheit)
# Create an instance of the KiloConverterGUI class.
temp_conv = TempConverterGUI()