2
2

Tkinter で作成する超適当な電卓

Posted at

初めに

プログラミング言語に関わらず、物事は、一先ずなんか作ったら習得しやすい!
という気がします。
というわけで、Python、TkinterでのGUIアプリケーション、「超適当な電卓」でございます。
なにか、参考になればです。
解説はしません。自分もよく理解してないんで!
私の友に言わせたら、"feeling to code is important!(?)"だそうです。

見て感じて書いて♪

それでは、コードを記載していきます♪

import tkinter as tk  # Tkinterをインポート

root = tk.Tk()  # アプリケーションウィンドウを作成

# メインクラスを作成
class MyFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        master.title("超適当な電卓")
        
        # 変数いろいろを作成
        self.current = 0
        self.term = 0
        self.option = 1
        
        # ボタン(1~9)を作成
        for c in range(3):
            for r in range(3):
                self.current += 1
                tk.Button(self.master, text=str(self.current), command=lambda x=self.current: self.key(x), width=2).grid(row=3-c, column=r)
        
        # 計算オプションのボタン(足し引きとか)を作成
        self.buttons = ["+", "-", "*", "/"]
        for i in range(len(self.buttons)):
            self.current += 1
            tk.Button(self.master, text=self.buttons[i], command=lambda x=self.current: self.key(x), width=2).grid(row=i+1, column=3)
        
        # 手抜きしてずっと self.current 使ってきたから、ここで再代入
        self.current = 0
        
        # 残りのボタン(0, =, C(clear))を作成
        tk.Button(self.master, text="0", command=lambda: self.key(0), width=2).grid(row=4, column=0)
        tk.Button(self.master, text="=", command=lambda: self.key(14), width=2).grid(row=3, column=4)
        tk.Button(self.master, text="C", command=lambda: self.key(15), width=2, bg="red").grid(row=1, column=4)
        
        # 計算結果とか表示するところ(エントリ)の作成
        self.entry = tk.Entry(self.master)
        self.entry.grid(row=0, column=0, columnspan=5)
        
    # 関数 [key] の作成
    def key(self, num):
        # 受け取った数字(num)によって処理をする。ようにする♪
        if num > 9:
            if num < 14:
                # オプションの、はい、足し引きとかのボタンの処理です。
                if num == 10: self.option = 1
                if num == 11: self.option = 2
                if num == 12: self.option = 3
                if num == 13: self.option = 4
                
                # このコード、もっと短くしたかったら、下記のようにもできる…ハズ
                #
                # もう、[if] を使用しない
                # self.option = num - 9
                # 以上
                
                self.term = self.current
                self.current = 0
            
            if num == 14 and self.term and self.current:
                # [=]のボタンがクリックされた時、かつ、[self.term]と[self.current]がある時の処理
                # どのオプションのボタンが押されてたかによって処理する♪
                if self.option == 1: self.current = self.term + self.current
                if self.option == 2: self.current = self.term - self.current
                if self.option == 3: self.current = self.term * self.current
                if self.option == 4: self.current = self.term // self.current  # ここでは、少数を扱うことを考えない!めんどいから!(超適当と言われる理由)よって、[/] ではなく、[//] を用いる
            
            if num == 15:
                # [C]のボタンがクリックされた時の処理
                self.term = self.current = 0
        
        else:
            # 押されたボタンが数字のやつ(0~1)だった時の処理
            self.current = self.current * 10 + num
        
        # entryにself.currentを表示させる
        # っと、その前に、entryにもともと入ってた文字列を削除しなきゃあね…
        self.entry.delete(0, tk.END)
        self.entry.insert(0, str(self.current))

frame = MyFrame(root)
frame.grid()

root.mainloop()  # メインループ


一応最後に(?)

image.png

このコードを実行したらば、こんなウィンドウがポップアップするハズ…です♪
以上♪

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2