Pythonはとても簡潔に書けるプログラミング言語だが、UIと実装を分けて、クラス毎にファイルを分割しないとコードが一つのファイルに数万行もあるなんてことになりかねず(自分も初学者のころはそんな感じだった)、PythonでUIと実装を分けるコードをAIに書かせてみた。
MVC(Model-View-Controller)であり、以下の3つのファイルからなる。
1.model.py(データ処理・ビジネスロジック)
2.controller.py (UIとModelをつなぐコントローラ)
3. view.py(UI部分)
Git Clone
git clone https://github.com/Sheephuman/Python-SepareteUI.git
※AIに書かせました
1. model.py(データ処理・ビジネスロジック)
# model.py
class CalculatorModel:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
def divide(self, a, b):
if b == 0:
return "Error: Division by zero"
return a / b
2.controller.py (UIとModelをつなぐコントローラ)
from model import CalculatorModel
class CalculatorController:
def __init__(self):
self.model = CalculatorModel()
def perform_operation(self, operation, a, b):
if operation == "add":
return self.model.add(a, b)
elif operation == "subtract":
return self.model.subtract(a, b)
elif operation == "multiply":
return self.model.multiply(a, b)
elif operation == "divide":
return self.model.divide(a, b)
3. view.py(UI部分)
# view.py
import tkinter as tk
from controller import CalculatorController
class CalculatorView:
def __init__(self, root):
self.root = root
self.root.title("Simple Calculator")
self.controller = CalculatorController()
# Entry fields for numbers
self.entry1 = tk.Entry(root)
self.entry1.grid(row=0, column=0, columnspan=2)
self.entry2 = tk.Entry(root)
self.entry2.grid(row=1, column=0, columnspan=2)
# Result label
self.result_label = tk.Label(root, text="Result")
self.result_label.grid(row=2, column=0, columnspan=2)
# Buttons for operations
self.add_button = tk.Button(root, text="Add", command=self.add)
self.add_button.grid(row=3, column=0)
self.subtract_button = tk.Button(root, text="Subtract", command=self.subtract)
self.subtract_button.grid(row=3, column=1)
self.multiply_button = tk.Button(root, text="Multiply", command=self.multiply)
self.multiply_button.grid(row=4, column=0)
self.divide_button = tk.Button(root, text="Divide", command=self.divide)
self.divide_button.grid(row=4, column=1)
def get_numbers(self):
try:
a = float(self.entry1.get())
b = float(self.entry2.get())
return a, b
except ValueError:
self.result_label.config(text="Error: Invalid input")
return None, None
def add(self):
a, b = self.get_numbers()
if a is not None and b is not None:
result = self.controller.perform_operation("add", a, b)
self.result_label.config(text=f"Result: {result}")
def subtract(self):
a, b = self.get_numbers()
if a is not None and b is not None:
result = self.controller.perform_operation("subtract", a, b)
self.result_label.config(text=f"Result: {result}")
def multiply(self):
a, b = self.get_numbers()
if a is not None and b is not None:
result = self.controller.perform_operation("multiply", a, b)
self.result_label.config(text=f"Result: {result}")
def divide(self):
a, b = self.get_numbers()
if a is not None and b is not None:
result = self.controller.perform_operation("divide", a, b)
self.result_label.config(text=f"Result: {result}")
if __name__ == "__main__":
root = tk.Tk()
view = CalculatorView(root)
root.mainloop()
作成した各ファイルは同じディレクトリに入れて、VSCodeで実行すればよい。
メインエントリは、view.py
の
if __name__ == "__main__":
root = tk.Tk() # tkinterのルートウィンドウを作成
view = CalculatorView(root) # CalculatorViewクラスのインスタンスを作成
root.mainloop() # tkinterのイベントループを開始
部分にあるため、View.pyを実行すればよい。
テキストボックスに数値を打ち込んでボタンを押すと計算結果を出力するもの。