質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

440閲覧

テキストフォルダを使った変換機能が作動しない

Mayaa

総合スコア1

import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/05/01 08:03

編集2022/01/12 10:55

前提・実現したいこと

テキストファイルの情報を基にGUIを作動させたい。

発生している問題・エラーメッセージ

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2021/05/01 08:31

このままではコードが読めないので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
Mayaa

2021/05/01 08:37

はい。ありがとうございます。修正致します。
Mayaa

2021/05/01 08:57

すいません。変更後の更新ボタンが作動していないようですので、再度新たに質問をし直します。
y_waiwai

2021/05/01 09:00

変更してないだけでは?
Mayaa

2021/05/01 13:14

ネットワークエラーだったようで、今出来ました。
guest

回答1

0

CM_TO_INCHESは定義されていますが、INCHES_TO_CMは定義されていないので、エラーになっています。

INCHES_TO_CMを定義しましょう。

投稿2021/05/01 08:33

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Mayaa

2021/05/01 13:12

初心者で詳しくないため、申し訳ありません・def INCHES_TO_CM と入力してみたのですが、変化がないのですが、コードに他の文言を足す必要があるでしょうか?
ppaul

2021/05/03 07:25

方法1 以下のように変更する。 INCHES_TO_CM = 0 def loadConversion(): global CM_TO_INCHES, INCHES_TO_CM f = open("operations.txt", "r") L1 = f.readline() val=L1.split(",") CM_TO_INCHES = float(val[1]) INCHES_TO_CM = 1 / CM_TO_INCHES def inchestocm(inches): inches *= INCHES_TO_CM return round(inches, 2) 方法2 INCHES_TO_CMを使うのを止めて、以下に変更する。 def inchestocm(inches): inches /= CM_TO_INCHES return round(inches, 2)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問