如何确定温度的计算
How to fix calculation for temperature
这是我的 python tkinter 公制转换计算器的代码。当数据运行时尝试转换数据时我遇到了问题。错误是不可散列的。非常感谢任何帮助 :) 并且将充分考虑建议。我相信错误在逻辑 class 中,只是不知道确切的位置。
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root):
self.notebook = ttk.Notebook(root)
self.notebook.grid(row = 0, column = 0)
self.length_frame = ttk.Frame(self.notebook)#Makes the length frame
self.weight_frame = ttk.Frame(self.notebook)#Makes the weight frame
self.temperature_frame = ttk.Frame(self.notebook)#Makes the temperature frame
#=====================================================================================================================================================================================================================================================
#Length Frame
self.notebook.add(self.length_frame, text = " Length ")
self.entrybox = StringVar()#Makes entrybox a string variable
self.Entry_length_input = ttk.Entry(self.length_frame, textvariable = self.entrybox, width = 13)
self.Entry_length_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx = 65, sticky = N)
#Creates the dropdown menu/combobox for input
self.measurements = StringVar()#Makes measurements a string variable
self.Combobox_Length_input = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ("Millimeters", "Centimeters", "Metres", "Kilometers", "Inches", "Feet", "Yards", "Miles"), width = 10, state = ["readonly"])#Creates the list options for the dropdown menu
self.Combobox_Length_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx = 10, sticky = N)
self.Label_Length_1 = ttk.Label(self.length_frame, text = "From")#Creates the from label
self.Label_Length_1.grid(row = 0, column = 1)
self.Label_Length_2 = ttk.Label(self.length_frame, text = "To")#Creates the to label
self.Label_Length_2.grid(row = 0, column = 5)
#Creats the dropdown menu/combobox for output
self.measurements1 = StringVar()#Makes measurements1 a string variable
self.Combobox_length_output = ttk.Combobox(self.length_frame, textvariable = self.measurements1, values = ("Millimeters", "Centimeters", "Metres", "Kilometers", "Inches", "Feet", "Yards", "Miles"), width = 10, state = ["readonly"])#Creates the list options for the dropdown menu
self.Combobox_length_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx = 10, sticky = N)
self.entrybox1 = StringVar()
self.Entry_length_output = ttk.Entry(self.length_frame, textvariable = self.entrybox1, width = 13, state = ["disabled"])#Makes the entrybox1 for the output not able to be edited
self.Entry_length_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)
self.Label_length_3 = ttk.Label(self.length_frame, text = "Input")#Creates the input label
self.Label_length_3.grid(row = 1, column = 0)
self.Label_length_4 = ttk.Label(self.length_frame, text = "Output")#Creates the output label
self.Label_length_4.grid(row = 1, column = 4)
self.convert_length_button = ttk.Button(self.length_frame, text = "Convert", command = self.convert_length)#Creates the convert button for length and does the calculation
self.convert_length_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)
#===========================================================================================================================================================================================================================================================
#Weight Frame
self.notebook.add(self.weight_frame, text = " Weight ")
self.entryboxweights = StringVar()
self.Entry_length_input = ttk.Entry(self.weight_frame, textvariable = self.entryboxweights, width = 13)
self.Entry_length_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx=65, sticky = N)
#Creates the dropdown menu/combobox for input
self.weights = StringVar()
self.Combobox_Length_input = ttk.Combobox(self.weight_frame, textvariable = self.weights, values = ("Grams", "Kilograms", "Ounces", "Pounds", "Tonnes"), width = 10, state = ["readonly"])
self.Combobox_Length_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx=10, sticky = N)
self.Label_weight_1 = ttk.Label(self.weight_frame, text = "From")
self.Label_weight_1.grid(row = 0, column = 1)
self.Label_weight_2 = ttk.Label(self.weight_frame, text = "To")
self.Label_weight_2.grid(row = 0, column = 5)
#Creates the dropdown menu/combobox for output
self.weights1 = StringVar()
self.Combobox_length_output = ttk.Combobox(self.weight_frame, textvariable = self.weights1, values = ("Grams", "Kilograms", "Ounces", "Pounds", "Tonnes"), width = 10, state = ["readonly"])
self.Combobox_length_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx =10, sticky = N)
self.entryboxweights2 = StringVar()
self.Entry_weight_output = ttk.Entry(self.weight_frame, textvariable = self.entryboxweights2, width = 13, state = ["disabled"])#Creates the weight ouput entry widget
self.Entry_weight_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)#Grids the weight output entry widget
self.Label_weight_3 = ttk.Label(self.weight_frame, text = "Input")#Creates the input label
self.Label_weight_3.grid(row = 1, column = 0)#Grids the input label
self.Label_weight_4 = ttk.Label(self.weight_frame, text = "Output")#Creates the output label
self.Label_weight_4.grid(row = 1, column = 4)#Grids the output label
self.convert_weight_button = ttk.Button(self.weight_frame, text = "Convert", command = self.convert_weight)#Creates the convert button and does the calculation
self.convert_weight_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)#Grids the convert button
#======================================================================================================================================================================================================
#Temperature Frame
self.notebook.add(self.temperature_frame, text = " Temperature ")
self.entryboxtemp = StringVar()
self.Entry_temp_input = ttk.Entry(self.temperature_frame, textvariable = self.entryboxtemp, width = 13)#Creates the temperature input entry widget
self.Entry_temp_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx=65, sticky = N)
self.temperature = StringVar()#Makes the temperature a string variable
self.Combobox_temp_input = ttk.Combobox(self.temperature_frame, textvariable = self.temperature, values = ("Celsius", "Fahrenheit", "Kelvin"), width = 10, state = ["readonly"])#Creates the combobox/dropdown for input
self.Combobox_temp_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx=10, sticky = N)
self.temperature2 = StringVar()
self.Combobox_temp_output = ttk.Combobox(self.temperature_frame, textvariable = self.temperature2, values = ("Celsius", "Fahrenheit", "Kelvin"), width = 10, state = ["readonly"])#Creates the combobox/dropdown for output
self.Combobox_temp_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx =10, sticky = N)
self.Label_temp_1 = ttk.Label(self.temperature_frame, text = "From")#Creates the label From
self.Label_temp_1.grid(row = 0, column = 1)
self.Label_temp_2 = ttk.Label(self.temperature_frame, text = "To")#Creates the label To
self.Label_temp_2.grid(row = 0, column = 5)
self.entryboxtemp2 = StringVar()
self.Entry_temp_output = ttk.Entry(self.temperature_frame, textvariable = self.entryboxtemp2, width = 13, state = ["disabled"], cursor = "left_ptr")#Makes the output entry widget not able to be edited
self.Entry_temp_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)
self.Label_temp_3 = ttk.Label(self.temperature_frame, text = "Input")#Creates the Input Label
self.Label_temp_3.grid(row = 1, column = 0)
self.Label_temp_4 = ttk.Label(self.temperature_frame, text = "Output")#Creates the Output Label
self.Label_temp_4.grid(row = 1, column = 4)
self.convert_temp_button = ttk.Button(self.temperature_frame, text = "Convert", command = self.convert_temperature)#Creates the convert button and does the calculation
self.convert_temp_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)#Grids the convert button
#============================================================================================================================================================================================================
class Logic(GUI):
#Calculates conversions for length
def convert_length(self, *args):
metre_dict = dict(Millimeters= 0.001, Centimeters= 0.01, Meters= 1, Kilometers= 1000, Inches= 0.0254, Feet= 0.3048, Yards= 0.9144, Miles= 1609.344)#Conversions for length
try:
v = float(self.entrybox.get())#Gets the values from the entry box and converts it into a float
except ValueError:
self.entrybox.set('')#Sets the entrybox to a blank
self.entrybox1.set('')#Sets the entrybox1 to a blank
return
m = v * metre_dict[self.measurements.get()]#Uses the
k = m / metre_dict[self.measurements1.get()]
self.entrybox1.set(k)
#Calculates conversions for weight
def convert_weight(self, *args):
gram_dict = dict(Grams= 1, Kilograms= 1000, Ounces= 28.34952313, Pounds= 453.59237, Tonnes= 1000000)#Conversions for weight
try:
v = float(self.entryboxweights.get())
except ValueError:
self.entryboxweights.set("")#Sets the entrybox for weights to a blank
self.entryboxweights1.set("")#Sets the entrybox weights1 to a blank
return
m = v * gram_dict[self.weights.get()]
k = m / gram_dict[self.weights1.get()]
self.entryboxweights2.set(k)
#Calculates conversions for temperature
def convert_temperature(self, *args):
Celsius = 1
Fahrenheit = Celsius*(9/5)+32
celsius_dict = dict(Celsius = 1, Fahrenheit = 33.8 , Kelvin = 274.15)#Conversions for temperature
try:
v = float(self.entryboxtemp.get())
except ValueError:
self.entryboxtemp.set('')#Sets the entrybox for temp to a blank
self.entryboxtemp1.set('')#Sets the entrybox for temp1 to a blank
return
m = v * celsius_dict[self.temperature.get()]
k = m / celsius_dict[self.temperature2.get()]
self.entryboxtemp2.set(k)
#=========================================================================================================================================================================================
#Main Routine
if __name__ == "__main__":
root = Tk()
app = Logic(root)
root.title("Wei Kit\'s Metric Calculator")#Title of the window
root.geometry("600x300")#Sets the size of the window
root.resizable(False, False)#Makes the window not resizable
root.mainloop()
看看这些行:
m = v * metre_dict[self.measurements]
k = m / metre_dict[self.measurements1]
self.measurements
和 self.measurements1
是 StringVars。为了获得它们的字符串值,您需要调用 get()
方法:
m = v * metre_dict[self.measurements.get()]
k = m / metre_dict[self.measurements1.get()]
如果你不放 .get(),你指的是看起来像这样的对象 <Tkinter.StringVar instance at 0x03000F30>
(基于 metre_dict,我猜你不会那)
这是我的 python tkinter 公制转换计算器的代码。当数据运行时尝试转换数据时我遇到了问题。错误是不可散列的。非常感谢任何帮助 :) 并且将充分考虑建议。我相信错误在逻辑 class 中,只是不知道确切的位置。
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root):
self.notebook = ttk.Notebook(root)
self.notebook.grid(row = 0, column = 0)
self.length_frame = ttk.Frame(self.notebook)#Makes the length frame
self.weight_frame = ttk.Frame(self.notebook)#Makes the weight frame
self.temperature_frame = ttk.Frame(self.notebook)#Makes the temperature frame
#=====================================================================================================================================================================================================================================================
#Length Frame
self.notebook.add(self.length_frame, text = " Length ")
self.entrybox = StringVar()#Makes entrybox a string variable
self.Entry_length_input = ttk.Entry(self.length_frame, textvariable = self.entrybox, width = 13)
self.Entry_length_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx = 65, sticky = N)
#Creates the dropdown menu/combobox for input
self.measurements = StringVar()#Makes measurements a string variable
self.Combobox_Length_input = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ("Millimeters", "Centimeters", "Metres", "Kilometers", "Inches", "Feet", "Yards", "Miles"), width = 10, state = ["readonly"])#Creates the list options for the dropdown menu
self.Combobox_Length_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx = 10, sticky = N)
self.Label_Length_1 = ttk.Label(self.length_frame, text = "From")#Creates the from label
self.Label_Length_1.grid(row = 0, column = 1)
self.Label_Length_2 = ttk.Label(self.length_frame, text = "To")#Creates the to label
self.Label_Length_2.grid(row = 0, column = 5)
#Creats the dropdown menu/combobox for output
self.measurements1 = StringVar()#Makes measurements1 a string variable
self.Combobox_length_output = ttk.Combobox(self.length_frame, textvariable = self.measurements1, values = ("Millimeters", "Centimeters", "Metres", "Kilometers", "Inches", "Feet", "Yards", "Miles"), width = 10, state = ["readonly"])#Creates the list options for the dropdown menu
self.Combobox_length_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx = 10, sticky = N)
self.entrybox1 = StringVar()
self.Entry_length_output = ttk.Entry(self.length_frame, textvariable = self.entrybox1, width = 13, state = ["disabled"])#Makes the entrybox1 for the output not able to be edited
self.Entry_length_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)
self.Label_length_3 = ttk.Label(self.length_frame, text = "Input")#Creates the input label
self.Label_length_3.grid(row = 1, column = 0)
self.Label_length_4 = ttk.Label(self.length_frame, text = "Output")#Creates the output label
self.Label_length_4.grid(row = 1, column = 4)
self.convert_length_button = ttk.Button(self.length_frame, text = "Convert", command = self.convert_length)#Creates the convert button for length and does the calculation
self.convert_length_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)
#===========================================================================================================================================================================================================================================================
#Weight Frame
self.notebook.add(self.weight_frame, text = " Weight ")
self.entryboxweights = StringVar()
self.Entry_length_input = ttk.Entry(self.weight_frame, textvariable = self.entryboxweights, width = 13)
self.Entry_length_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx=65, sticky = N)
#Creates the dropdown menu/combobox for input
self.weights = StringVar()
self.Combobox_Length_input = ttk.Combobox(self.weight_frame, textvariable = self.weights, values = ("Grams", "Kilograms", "Ounces", "Pounds", "Tonnes"), width = 10, state = ["readonly"])
self.Combobox_Length_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx=10, sticky = N)
self.Label_weight_1 = ttk.Label(self.weight_frame, text = "From")
self.Label_weight_1.grid(row = 0, column = 1)
self.Label_weight_2 = ttk.Label(self.weight_frame, text = "To")
self.Label_weight_2.grid(row = 0, column = 5)
#Creates the dropdown menu/combobox for output
self.weights1 = StringVar()
self.Combobox_length_output = ttk.Combobox(self.weight_frame, textvariable = self.weights1, values = ("Grams", "Kilograms", "Ounces", "Pounds", "Tonnes"), width = 10, state = ["readonly"])
self.Combobox_length_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx =10, sticky = N)
self.entryboxweights2 = StringVar()
self.Entry_weight_output = ttk.Entry(self.weight_frame, textvariable = self.entryboxweights2, width = 13, state = ["disabled"])#Creates the weight ouput entry widget
self.Entry_weight_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)#Grids the weight output entry widget
self.Label_weight_3 = ttk.Label(self.weight_frame, text = "Input")#Creates the input label
self.Label_weight_3.grid(row = 1, column = 0)#Grids the input label
self.Label_weight_4 = ttk.Label(self.weight_frame, text = "Output")#Creates the output label
self.Label_weight_4.grid(row = 1, column = 4)#Grids the output label
self.convert_weight_button = ttk.Button(self.weight_frame, text = "Convert", command = self.convert_weight)#Creates the convert button and does the calculation
self.convert_weight_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)#Grids the convert button
#======================================================================================================================================================================================================
#Temperature Frame
self.notebook.add(self.temperature_frame, text = " Temperature ")
self.entryboxtemp = StringVar()
self.Entry_temp_input = ttk.Entry(self.temperature_frame, textvariable = self.entryboxtemp, width = 13)#Creates the temperature input entry widget
self.Entry_temp_input.grid(row = 1, column = 1, padx = 1, pady = 5, ipadx=65, sticky = N)
self.temperature = StringVar()#Makes the temperature a string variable
self.Combobox_temp_input = ttk.Combobox(self.temperature_frame, textvariable = self.temperature, values = ("Celsius", "Fahrenheit", "Kelvin"), width = 10, state = ["readonly"])#Creates the combobox/dropdown for input
self.Combobox_temp_input.grid(row = 3, column = 1, padx = 2, pady = 5, ipadx=10, sticky = N)
self.temperature2 = StringVar()
self.Combobox_temp_output = ttk.Combobox(self.temperature_frame, textvariable = self.temperature2, values = ("Celsius", "Fahrenheit", "Kelvin"), width = 10, state = ["readonly"])#Creates the combobox/dropdown for output
self.Combobox_temp_output.grid(row = 3, column = 5, padx =5, pady = 5, ipadx =10, sticky = N)
self.Label_temp_1 = ttk.Label(self.temperature_frame, text = "From")#Creates the label From
self.Label_temp_1.grid(row = 0, column = 1)
self.Label_temp_2 = ttk.Label(self.temperature_frame, text = "To")#Creates the label To
self.Label_temp_2.grid(row = 0, column = 5)
self.entryboxtemp2 = StringVar()
self.Entry_temp_output = ttk.Entry(self.temperature_frame, textvariable = self.entryboxtemp2, width = 13, state = ["disabled"], cursor = "left_ptr")#Makes the output entry widget not able to be edited
self.Entry_temp_output.grid(row = 1, column = 5, padx = 5, pady = 5, ipadx = 65, sticky = N)
self.Label_temp_3 = ttk.Label(self.temperature_frame, text = "Input")#Creates the Input Label
self.Label_temp_3.grid(row = 1, column = 0)
self.Label_temp_4 = ttk.Label(self.temperature_frame, text = "Output")#Creates the Output Label
self.Label_temp_4.grid(row = 1, column = 4)
self.convert_temp_button = ttk.Button(self.temperature_frame, text = "Convert", command = self.convert_temperature)#Creates the convert button and does the calculation
self.convert_temp_button.grid(row = 1, column = 7, padx = 5, pady = 5, sticky = N)#Grids the convert button
#============================================================================================================================================================================================================
class Logic(GUI):
#Calculates conversions for length
def convert_length(self, *args):
metre_dict = dict(Millimeters= 0.001, Centimeters= 0.01, Meters= 1, Kilometers= 1000, Inches= 0.0254, Feet= 0.3048, Yards= 0.9144, Miles= 1609.344)#Conversions for length
try:
v = float(self.entrybox.get())#Gets the values from the entry box and converts it into a float
except ValueError:
self.entrybox.set('')#Sets the entrybox to a blank
self.entrybox1.set('')#Sets the entrybox1 to a blank
return
m = v * metre_dict[self.measurements.get()]#Uses the
k = m / metre_dict[self.measurements1.get()]
self.entrybox1.set(k)
#Calculates conversions for weight
def convert_weight(self, *args):
gram_dict = dict(Grams= 1, Kilograms= 1000, Ounces= 28.34952313, Pounds= 453.59237, Tonnes= 1000000)#Conversions for weight
try:
v = float(self.entryboxweights.get())
except ValueError:
self.entryboxweights.set("")#Sets the entrybox for weights to a blank
self.entryboxweights1.set("")#Sets the entrybox weights1 to a blank
return
m = v * gram_dict[self.weights.get()]
k = m / gram_dict[self.weights1.get()]
self.entryboxweights2.set(k)
#Calculates conversions for temperature
def convert_temperature(self, *args):
Celsius = 1
Fahrenheit = Celsius*(9/5)+32
celsius_dict = dict(Celsius = 1, Fahrenheit = 33.8 , Kelvin = 274.15)#Conversions for temperature
try:
v = float(self.entryboxtemp.get())
except ValueError:
self.entryboxtemp.set('')#Sets the entrybox for temp to a blank
self.entryboxtemp1.set('')#Sets the entrybox for temp1 to a blank
return
m = v * celsius_dict[self.temperature.get()]
k = m / celsius_dict[self.temperature2.get()]
self.entryboxtemp2.set(k)
#=========================================================================================================================================================================================
#Main Routine
if __name__ == "__main__":
root = Tk()
app = Logic(root)
root.title("Wei Kit\'s Metric Calculator")#Title of the window
root.geometry("600x300")#Sets the size of the window
root.resizable(False, False)#Makes the window not resizable
root.mainloop()
看看这些行:
m = v * metre_dict[self.measurements]
k = m / metre_dict[self.measurements1]
self.measurements
和 self.measurements1
是 StringVars。为了获得它们的字符串值,您需要调用 get()
方法:
m = v * metre_dict[self.measurements.get()]
k = m / metre_dict[self.measurements1.get()]
如果你不放 .get(),你指的是看起来像这样的对象 <Tkinter.StringVar instance at 0x03000F30>
(基于 metre_dict,我猜你不会那)