teratail header banner
teratail header banner
質問するログイン新規登録

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

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

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

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

Python

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

Q&A

解決済

1回答

915閲覧

pythonでtkinterを使って二次関数を解くアプリを作ろうとしています。

haru0

総合スコア5

Tkinter

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

Python

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

0グッド

0クリップ

投稿2022/07/21 21:41

編集2022/07/21 22:48

0

0

前提

pythonでtkinterを使って二次関数を解くアプリを作ろうとしています。それなりの初心者です。Teratailも今回初めて使います。よろしくお願いします。

実現したいこと

使用者がax^2 + bx + c = 0のa, b, cに好きな数字を入力し、それをsympyで計算し、その結果を表示させる。入力できるのは数字だけにもしたい。
自分的にはこの方法が正しいと思っているが、本当にそうかは自信ない。

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

tkinterで入力した数字をa, b, cで受け取り、計算する方法が分からない。

エラーメッセージ

### 該当のソースコード python ソースコード import sympy as sym a, b, c, x = sym.symbols("a b c x") eq = sym.Eq(a * x ** 2 + b * x + c) sym.solve(eq, x) ----------------------------------- from tkinter import * from tkinter import ttk from tkinter import messagebox as mb root = Tk() root.title("Quadratic Equations Calculator") root.geometry('400x400') la1 = ttk.Label(root, text ='ax^2 + bx + c = 0') la1.pack() la2 = ttk.Label(root, text ='Enter values for a, b and c:') la2.pack() la3 = ttk.Label(root, text ='a =') la3.pack() en3 = ttk.Entry(root) en3.pack() la4 = ttk.Label(root, text ='b =') la4.pack() en4 = ttk.Entry(root) en4.pack() la5 = ttk.Label(root, text ='c =') la5.pack() en5 = ttk.Entry(root) en5.pack() computebutton = ttk.Button(root, text ='Compute') computebutton.pack() def help_button(): mb.showinfo('Help', 'You can only enter integers.') helpbu = ttk.Button(root, text ='Help', command = help_button) helpbu.pack() def quit_button(): root.destroy() quitbu = ttk.Button(root, text ='Quit', command = quit_button) quitbu.pack() root.mainloop() ### 試したこと お恥ずかしいことにこの先何をすればいいのか全く分かりません。完全に詰まっています。すみません。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

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

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

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

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

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

RiaFeed

2022/07/21 21:44

自分的にはこの方法が正しいと思っているソースコードはどこにあるのでしょう?
y_waiwai

2022/07/21 22:10

コードが提示されてません。 質問文は編集できるんで修正しましょう
dodox86

2022/07/22 00:18

> お恥ずかしいことにこの先何をすればいいのか全く分かりません。完全に詰まっています。すみません。 a, b, cを個別に入力するUIをtkinterで作って、pythonの関数やメソッドにするなりした二次関数の実行処理を行って結果を表示するようにすれば良いのでは。
haru0

2022/07/22 00:32

すみません。もうすこし詳しく教えてもらえないでしょうか?初学者でいまいち理解できません。お願いします。
guest

回答1

0

ベストアンサー

これで一応動くと思うわ!

Python

1### 該当のソースコード 2 3import sympy as sym 4 5from tkinter import * 6from tkinter import ttk 7from tkinter import messagebox as mb 8 9class gui(): 10 def __init__(self, master=None): 11 12 la1 = ttk.Label(root, text ='ax^2 + bx + c = 0') 13 la1.pack() 14 15 la2 = ttk.Label(root, text ='Enter values for a, b and c:') 16 la2.pack() 17 18 la3 = ttk.Label(root, text ='a =') 19 la3.pack() 20 self.en3 = ttk.Entry(root) 21 self.en3.pack() 22 23 la4 = ttk.Label(root, text ='b =') 24 la4.pack() 25 self.en4 = ttk.Entry(root) 26 self.en4.pack() 27 28 la5 = ttk.Label(root, text ='c =') 29 la5.pack() 30 self.en5 = ttk.Entry(root) 31 self.en5.pack() 32 33 computebutton = ttk.Button(root, text ='Compute', command = self.compute) 34 computebutton.pack() 35 36 helpbu = ttk.Button(root, text ='Help', command = self.help_button) 37 helpbu.pack() 38 39 quitbu = ttk.Button(root, text ='Quit', command = self.quit_button) 40 quitbu.pack() 41 42 self.la6 = ttk.Label(root, text ='x = ') 43 self.la6.pack() 44 45 def compute(self): 46 a = self.en3.get() 47 b = self.en4.get() 48 c = self.en5.get() 49 sym.init_printing() 50 a, b, c, x = sym.symbols("{} {} {} x".format(a, b, c)) 51 ans = sym.solveset(a*x**2 + b*x + c, x) 52 self.la6.configure(text = "x = " + str(ans)) 53 print(type(ans)) 54 55 def help_button(self): 56 mb.showinfo('Help', 'You can only enter integers.') 57 58 def quit_button(self): 59 root.destroy() 60 61if __name__ == '__main__': 62 root = Tk() 63 root.title("Quadratic Equations Calculator") 64 root.geometry('400x400+0+0') 65 gui(root) 66 root.mainloop() 67

追記:

sym.solveset() の戻り値は数式みたいだから eval() で計算してみたわ。
でもこのコードだと解が虚数のときは解けないわ。

整数のみで表示する方法だけれど、float型の変数が整数かどうかをis_integer()で調べて、Trueの場合はint型に変換してみたわ。
Classを使った理由は見やすくするためよ。

from tkinter import * from tkinter import ttk from tkinter import messagebox import sympy as sym from numpy import sqrt def isOk(diff): if not diff.encode('utf-8').isdigit(): # 妥当でない(半角数字でない)場合はFalseを返却 return False # 妥当(半角数字である)の場合はTrueを返却 return True root = Tk() tcl_isOk = root.register(isOk) root.title("Quadratic Equations Calculator") root.geometry('400x400') la1 = ttk.Label(root, text ='ax^2 + bx + c = 0') la1.pack(pady=20) la2 = ttk.Label(root, text ='Enter values for a, b and c:') la2.pack(pady=20) la3 = ttk.Label(root, text ='a =') la3.pack() en3 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en3.pack() la4 = ttk.Label(root, text ='b =') la4.pack() en4 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en4.pack() la5 = ttk.Label(root, text ='c =') la5.pack() en5 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en5.pack() def compute_button(): a = en3.get() b = en4.get() c = en5.get() c = -1 #sym.init_printing() a, b, c, x = sym.symbols("{} {} {} x".format(a, b, c)) ans = sym.solveset(a*x**2 + b*x + c, x) ans_1 = eval(str(list(ans)[0])) ans_2 = eval(str(list(ans)[1])) if ans_1.is_integer(): la6.configure(text = "x = " + str(int(ans_1))) else: la6.configure(text = "x = " + str(ans_1)) if ans_1.is_integer(): la6.configure(text = "x = " + str(int(ans_2))) else: la6.configure(text = "x = " + str(ans_2)) computebutton = ttk.Button(root, text ='Compute', command = compute_button) computebutton.pack() def help_button(): messagebox.showinfo('Help', 'You can only enter integers.') helpbutton = ttk.Button(root, text ='Help', command = help_button) helpbutton.pack(pady=3) def quit_button(): root.destroy() quitbutton = ttk.Button(root, text ='Quit', command = quit_button) quitbutton.pack(pady=3) la6 = ttk.Label(root, text ='x = ') la6.pack() la7 = ttk.Label(root, text ='x = ') la7.pack() root.mainloop()

投稿2022/07/22 01:55

編集2022/07/22 05:42
satourin

総合スコア20

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

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

haru0

2022/07/22 03:43

本当にありがとうございます。 コードが動いて無茶苦茶嬉しいです。 重ねて質問なのですが、解が整数のとき整数のみで表示するにはどうすればいいですか?またclassを使う理由はなんですか?コードを見やすくするためですか? 一応自分のコードです。入力制限で数字しか入力出来ない以外は同じ度と思います。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー from tkinter import * from tkinter import ttk from tkinter import messagebox import sympy as sym def isOk(diff): if not diff.encode('utf-8').isdigit(): # 妥当でない(半角数字でない)場合はFalseを返却 return False # 妥当(半角数字である)の場合はTrueを返却 return True root = Tk() tcl_isOk = root.register(isOk) root.title("Quadratic Equations Calculator") root.geometry('400x400') la1 = ttk.Label(root, text ='ax^2 + bx + c = 0') la1.pack(pady=20) la2 = ttk.Label(root, text ='Enter values for a, b and c:') la2.pack(pady=20) la3 = ttk.Label(root, text ='a =') la3.pack() en3 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en3.pack() la4 = ttk.Label(root, text ='b =') la4.pack() en4 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en4.pack() la5 = ttk.Label(root, text ='c =') la5.pack() en5 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S')) en5.pack() def compute_button(): a = en3.get() b = en4.get() c = en5.get() sym.init_printing() a, b, c, x = sym.symbols("{} {} {} x".format(a, b, c)) ans = sym.solveset(a*x**2 + b*x + c, x) la6.configure(text = "x = " + str(ans)) print(type(ans)) computebutton = ttk.Button(root, text ='Compute', command = compute_button) computebutton.pack() def help_button(): messagebox.showinfo('Help', 'You can only enter integers.') helpbutton = ttk.Button(root, text ='Help', command = help_button) helpbutton.pack(pady=3) def quit_button(): root.destroy() quitbutton = ttk.Button(root, text ='Quit', command = quit_button) quitbutton.pack(pady=3) la6 = ttk.Label(root, text ='x = ') la6.pack() root.mainloop()
haru0

2022/07/22 20:15

本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.30%

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

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

質問する

関連した質問