前提・実現したいこと
Pythonでtkinterを用いて整数の四則演算のみの電卓を作成しています。
電卓機能のクラスDentakuとそれ以外の数値の入力、ウィジェットの設定を定義するクラスMyFrameを組み合わせて作ろうとしています。
発生している問題・エラーメッセージ
ボタンで数値(first_term,second_term)入力をしても電卓機能のクラスDentakuに継承されない。
first_term,second_termともにprintすると0と出力される。
該当のソースコード
Python
1import tkinter as tk 2 3# 第一項 4first_term = 0 5# 第二項 6second_term = 0 7# 結果 8result = 0 9# 入力中の数字 10current_number = 0 11 12 13def do_plus(): 14 global current_number 15 global first_term 16 global ennzannshi 17 first_term = current_number 18 current_number = 0 19 ennzannshi = "+" 20 21 22def do_eq(): 23 global second_term 24 global result 25 global current_number 26 global ennzannshi 27 second_term = current_number 28 if ennzannshi == "-": 29 result = first_term - second_term 30 elif ennzannshi == "+": 31 result = first_term + second_term 32 elif ennzannshi == "*": 33 result = first_term * second_term 34 elif ennzannshi == "/" and second_term == 0: 35 pass 36 else: 37 result = first_term // second_term 38 current_number = 0 39 40 41class Dentaku(): 42 def __init__(self): 43 self.first_term = 0 44 self.second_term = 0 45 self.second_term = 0 46 self.result = 0 47 self.operation = "+" 48 49 def do_operation(self): 50 if self.operation == "+": 51 self.result = self.first_term + self.second_term 52 elif self.operation == "-": 53 self.result = self.first_term - self.second_term 54 elif self.operation == "*": 55 self.result = self.first_term * self.second_term 56 elif self.operation == "/": 57 self.result = self.first_term // self.second_term 58 59 60 61class MyFrame(tk.Frame, Dentaku): 62 def __init__(self, master=None): 63 super().__init__(master) 64 65 self.first_term = dentaku.first_term 66 self.second_term = dentaku.second_term 67 b1 = tk.Button(self, text='1', command=lambda: self.key(1), bg='#ffffff', width=2, font=('Helvetica', 14)) 68 b2 = tk.Button(self, text='2', command=lambda: self.key(2), bg='#ffffff', width=2, font=('Helvetica', 14)) 69 b3 = tk.Button(self, text='3', command=lambda: self.key(3), bg='#ffffff', width=2, font=('Helvetica', 14)) 70 b4 = tk.Button(self, text='4', command=lambda: self.key(4), bg='#ffffff', width=2, font=('Helvetica', 14)) 71 b5 = tk.Button(self, text='5', command=lambda: self.key(5), bg='#ffffff', width=2, font=('Helvetica', 14)) 72 b6 = tk.Button(self, text='6', command=lambda: self.key(6), bg='#ffffff', width=2, font=('Helvetica', 14)) 73 b7 = tk.Button(self, text='7', command=lambda: self.key(7), bg='#ffffff', width=2, font=('Helvetica', 14)) 74 b8 = tk.Button(self, text='8', command=lambda: self.key(8), bg='#ffffff', width=2, font=('Helvetica', 14)) 75 b9 = tk.Button(self, text='9', command=lambda: self.key(9), bg='#ffffff', width=2, font=('Helvetica', 14)) 76 b0 = tk.Button(self, text='0', command=lambda: self.key(0), bg='#ffffff', width=2, font=('Helvetica', 14)) 77 bc = tk.Button(self, text='C', command=self.clear, bg='#ff0000', width=2, font=('Helvetica', 14)) 78 bp = tk.Button(self, text='+', command=self.plus, bg='#00ff00', width=2, font=('Helvetica', 14)) 79 bm = tk.Button(self, text='-', command=self.minus, bg='#00ff00', width=2, font=('Helvetica', 14)) 80 bmp = tk.Button(self, text='*', command=self.multiply, bg='#00ff00', width=2, font=('Helvetica', 14)) 81 bd = tk.Button(self, text='/', command=self.division, bg='#00ff00', width=2, font=('Helvetica', 14)) 82 be = tk.Button(self, text="=", command=self.eq, bg='#00ff00', width=2, font=('Helvetica', 14)) 83 84 b1.grid(row=3, column=0) 85 b2.grid(row=3, column=1) 86 b3.grid(row=3, column=2) 87 b4.grid(row=2, column=0) 88 b5.grid(row=2, column=1) 89 b6.grid(row=2, column=2) 90 b7.grid(row=1, column=0) 91 b8.grid(row=1, column=1) 92 b9.grid(row=1, column=2) 93 b0.grid(row=4, column=0) 94 bc.grid(row=1, column=3) 95 be.grid(row=4, column=3) 96 bp.grid(row=2, column=3) 97 bm.grid(row=3, column=3) 98 bmp.grid(row=4, column=1) 99 bd.grid(row=4, column=2) 100 101 self.e = tk.Entry(self, font=('Helvetica', 14)) 102 self.e.grid(row=0, column=0, columnspan=4) 103 104 def key(self, n): 105 global current_number 106 current_number = current_number * 10 + n 107 self.show_number(current_number) 108 109 def clear(self): 110 global current_number 111 current_number = 0 112 self.show_number(current_number) 113 114 def plus(self): 115 print(dentaku.first_term) 116 do_plus() 117 dentaku.operation = "+" 118 print(dentaku.operation) 119 print(dentaku.second_term) 120 dentaku.do_operation() 121 print(dentaku.result) 122 self.show_number(dentaku.result) 123 124 125 def eq(self): 126 do_eq() 127 self.show_number(dentaku.result) 128 129 def show_number(self, num): 130 self.e.delete(0, tk.END) 131 self.e.insert(0, str(num)) 132 133 134dentaku = Dentaku() 135root = tk.Tk() 136f = MyFrame(root) 137f.pack() 138f.mainloop() 139
試したこと
表示されるウィンドウの+ボタンを押下すると
0
"+"
0
0
と出力されます
![guest](/img/icon/icnUserSample.jpg)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/25 16:01