>

다음은 파이썬 tkinter 메트릭 변환 계산기의 코드입니다. 길이가 길어지면 데이터를 변환하려고 할 때 문제가 있습니다. 오류는 해시 할 수 없습니다. 모든 도움을 주시면 감사하겠습니다. 오류가 Logic 클래스에 있다고 생각합니다. 정확히 어디에 있는지 모릅니다.

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()

  • 답변 # 1

    다음 행을 살펴보십시오 :

    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에 따르면, 당신은 그렇게하지 않을 것이라고 생각합니다)

  • 이전 c# - Visual Studio 2019에서 FolderBrowserDialog를 사용하려면 어떻게해야합니까?
  • 다음 dll - Java에서 창을 앞으로 가져 오기