回答編集履歴
1
追記
test
CHANGED
@@ -69,3 +69,99 @@
|
|
69
69
|
root.mainloop()
|
70
70
|
|
71
71
|
```
|
72
|
+
---
|
73
|
+
追記:
|
74
|
+
|
75
|
+
|
76
|
+
sym.solveset() の戻り値は数式みたいだから eval() で計算してみたわ。
|
77
|
+
でもこのコードだと解が虚数のときは解けないわ。
|
78
|
+
|
79
|
+
整数のみで表示する方法だけれど、float型の変数が整数かどうかをis_integer()で調べて、Trueの場合はint型に変換してみたわ。
|
80
|
+
Classを使った理由は見やすくするためよ。
|
81
|
+
|
82
|
+
```
|
83
|
+
from tkinter import *
|
84
|
+
from tkinter import ttk
|
85
|
+
from tkinter import messagebox
|
86
|
+
import sympy as sym
|
87
|
+
from numpy import sqrt
|
88
|
+
|
89
|
+
def isOk(diff):
|
90
|
+
if not diff.encode('utf-8').isdigit():
|
91
|
+
# 妥当でない(半角数字でない)場合はFalseを返却
|
92
|
+
return False
|
93
|
+
# 妥当(半角数字である)の場合はTrueを返却
|
94
|
+
return True
|
95
|
+
|
96
|
+
root = Tk()
|
97
|
+
|
98
|
+
tcl_isOk = root.register(isOk)
|
99
|
+
|
100
|
+
root.title("Quadratic Equations Calculator")
|
101
|
+
root.geometry('400x400')
|
102
|
+
|
103
|
+
la1 = ttk.Label(root, text ='ax^2 + bx + c = 0')
|
104
|
+
la1.pack(pady=20)
|
105
|
+
|
106
|
+
la2 = ttk.Label(root, text ='Enter values for a, b and c:')
|
107
|
+
la2.pack(pady=20)
|
108
|
+
|
109
|
+
|
110
|
+
la3 = ttk.Label(root, text ='a =')
|
111
|
+
la3.pack()
|
112
|
+
en3 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S'))
|
113
|
+
en3.pack()
|
114
|
+
|
115
|
+
la4 = ttk.Label(root, text ='b =')
|
116
|
+
la4.pack()
|
117
|
+
en4 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S'))
|
118
|
+
en4.pack()
|
119
|
+
|
120
|
+
la5 = ttk.Label(root, text ='c =')
|
121
|
+
la5.pack()
|
122
|
+
en5 = ttk.Entry(root, validate='key', validatecommand=(tcl_isOk, '%S'))
|
123
|
+
en5.pack()
|
124
|
+
|
125
|
+
def compute_button():
|
126
|
+
a = en3.get()
|
127
|
+
b = en4.get()
|
128
|
+
c = en5.get()
|
129
|
+
c = -1
|
130
|
+
#sym.init_printing()
|
131
|
+
a, b, c, x = sym.symbols("{} {} {} x".format(a, b, c))
|
132
|
+
ans = sym.solveset(a*x**2 + b*x + c, x)
|
133
|
+
ans_1 = eval(str(list(ans)[0]))
|
134
|
+
ans_2 = eval(str(list(ans)[1]))
|
135
|
+
if ans_1.is_integer():
|
136
|
+
la6.configure(text = "x = " + str(int(ans_1)))
|
137
|
+
else:
|
138
|
+
la6.configure(text = "x = " + str(ans_1))
|
139
|
+
if ans_1.is_integer():
|
140
|
+
la6.configure(text = "x = " + str(int(ans_2)))
|
141
|
+
else:
|
142
|
+
la6.configure(text = "x = " + str(ans_2))
|
143
|
+
|
144
|
+
computebutton = ttk.Button(root, text ='Compute', command = compute_button)
|
145
|
+
computebutton.pack()
|
146
|
+
|
147
|
+
|
148
|
+
def help_button():
|
149
|
+
messagebox.showinfo('Help', 'You can only enter integers.')
|
150
|
+
|
151
|
+
helpbutton = ttk.Button(root, text ='Help', command = help_button)
|
152
|
+
helpbutton.pack(pady=3)
|
153
|
+
|
154
|
+
def quit_button():
|
155
|
+
root.destroy()
|
156
|
+
|
157
|
+
quitbutton = ttk.Button(root, text ='Quit', command = quit_button)
|
158
|
+
quitbutton.pack(pady=3)
|
159
|
+
|
160
|
+
la6 = ttk.Label(root, text ='x = ')
|
161
|
+
la6.pack()
|
162
|
+
|
163
|
+
la7 = ttk.Label(root, text ='x = ')
|
164
|
+
la7.pack()
|
165
|
+
|
166
|
+
root.mainloop()
|
167
|
+
```
|