##前回も同じ質問をしましたが、コードの記載場所のミスを修正したところ、「更新ボタン」が機能しなかったため、再度新たに質問をさせて頂きます。
##<目的>
##外部テキストを使用して、計算機能ツールを作動させたい。
##<現状>
#cm-->inchesは作動します。
#inches --->cmはエラーが表示されてしまします。
#Python
発生している問題・エラーメッセージ
#cmからインチへの変換は出来るのに、インチからCmを試すと以下のエラーが表示されてしまいます。
#<エラーメッセージ>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Maya\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "F:\NewStage3.py", line 44, in conversion
num = inchestocm(float(txtInput.get()))
File "F:\NewStage3.py", line 17, in inchestocm
inches /= INCHES_TO_CM
NameError: name 'INCHES_TO_CM' is not defined
pythonに挿入用のテキストファイルのコンテンツ
cm_to_inches, 0.394
コード ## Date created: 30 April 2021 ## Date last changed: 29 April 2021 ## This Program does to convert cm to inches, and inches to cm. ## Input:None, Output:None CM_TO_INCHES = 0 #Crearing global valuable # Import module from tkinter import * def cmtoinches(cm): cm *= CM_TO_INCHES return round(cm, 2) def inchestocm(inches): inches /= INCHES_TO_CM return round(inches, 2) def loadConversion(): global CM_TO_INCHES f = open("operations.txt", "r") L1 = f.readline() val=L1.split(",") CM_TO_INCHES = float(val[1]) #Create object loadConversion() root = Tk() root.title("convert length") root.configure(background="#EEEFFF") theLabel = Label(root, text="This is a cm/inches conversion, please select the unit, and add length you wish to convert to") # Adjust size # root.geometry( "200x200" ) # Change the label text def conversion(): num = 0 if (clicked.get() == "cm"): num = cmtoinches(float(txtInput.get())) lblOutput.config( text = str(num) + " inches" ) else: num = inchestocm(float(txtInput.get())) lblOutput.config( text = str(num) + " cm" ) def changeInput(value): if (clicked.get() == "cm"): lblInput.config( text = "Convert to inches" ) else: lblInput.config( text = "Convert to cm" ) # def conversion(): # if clicked # Dropdown menu options options = [ "cm", "inches" ] # datatype of menu text clicked = StringVar() # initial menu text clicked.set( options[0] ) lblValue = Label(root, text="Insert a value").pack() txtInput =StringVar() e1=Entry(root, textvariable=txtInput).pack() lblInput = Label( root , text="inches" ) lblInput.pack() lblSelect = Label(root, text="Select unit to be converted into").pack() # Create Dropdown menu drop = OptionMenu( root , clicked , *options, command=changeInput) drop.pack() # Create button, it will change label text btnConvert = Button(root , text = "convert" , command = conversion ).pack() btnExit =Button(root, text="click to exit", command=root.destroy).pack() # Create Label lblOutput = Label( root , text = "", fg="#0000FF" ) lblOutput.pack() # Execute tkinter root.mainloop()
回答2件
あなたの回答
tips
プレビュー