Python 入門指南 5.0

exercise4310.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 從 Brython 引入 document 並以 doc 為別名
from browser import document as doc

# 暫存輸入
command = ""

# 狀態變數
result = False

# 進行按鈕 1
def method_1(event):
    global command, result
    if not result:
        command += "1"
    else:
        command = "1"
        result = False
    doc["display"].text = command

# 進行按鈕 2
def method_2(event):
    global command, result
    if not result:
        command += "2"
    else:
        command = "2"
        result = False
    doc["display"].text = command

# 進行按鈕 3
def method_3(event):
    global command, result
    if not result:
        command += "3"
    else:
        command = "3"
        result = False
    doc["display"].text = command

# 進行按鈕 4
def method_4(event):
    global command, result
    if not result:
        command += "4"
    else:
        command = "4"
        result = False
    doc["display"].text = command

# 進行按鈕 5
def method_5(event):
    global command, result
    if not result:
        command += "5"
    else:
        command = "5"
        result = False
    doc["display"].text = command

# 進行按鈕 6
def method_6(event):
    global command, result
    if not result:
        command += "6"
    else:
        command = "6"
        result = False
    doc["display"].text = command

# 進行按鈕 7
def method_7(event):
    global command, result
    if not result:
        command += "7"
    else:
        command = "7"
        result = False
    doc["display"].text = command

# 進行按鈕 8
def method_8(event):
    global command, result
    if not result:
        command += "8"
    else:
        command = "8"
        result = False
    doc["display"].text = command

# 進行按鈕 9
def method_9(event):
    global command, result
    if not result:
        command += "9"
    else:
        command = "9"
        result = False
    doc["display"].text = command

# 進行按鈕 0
def method_0(event):
    global command, result
    if not result:
        command += "0"
    else:
        command = "0"
        result = False
    doc["display"].text = command

# 進行按鈕 .
def method_dot(event):
    global command
    command += "."
    doc["display"].text = command

# 進行按鈕 +
def method_add(event):
    global command, result
    if result:
        result = False
    command += "+"
    doc["display"].text = command

# 進行按鈕 -
def method_mimus(event):
    global command, result
    if result:
        result = False
    command += "-"
    doc["display"].text = command

# 進行按鈕 *
def method_multiply(event):
    global command, result
    if result:
        result = False
    command += "*"
    doc["display"].text = command

# 進行按鈕 /
def method_divide(event):
    global command, result
    if result:
        result = False
    command += "/"
    doc["display"].text = command

# 進行按鈕 =
def method_equal(event):
    global command, result
    if command == "":
        doc["display"].text = "0"
    else:
        command = str(eval(command))
        doc["display"].text = command
        result = True

# 按鈕註冊事件
doc["button1"].bind("click", method_1)
doc["button2"].bind("click", method_2)
doc["button3"].bind("click", method_3)
doc["button4"].bind("click", method_4)
doc["button5"].bind("click", method_5)
doc["button6"].bind("click", method_6)
doc["button7"].bind("click", method_7)
doc["button8"].bind("click", method_8)
doc["button9"].bind("click", method_9)
doc["button0"].bind("click", method_0)
doc["button_dot"].bind("click", method_dot)
doc["button_add"].bind("click", method_add)
doc["button_minus"].bind("click", method_mimus)
doc["button_multiply"].bind("click", method_multiply)
doc["button_divide"].bind("click", method_divide)
doc["button_equal"].bind("click", method_equal)

# 檔名: exercise4310.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 12 月

回到練習題目
上一頁 exercise4309.py
回 Python 入門指南 5.0 首頁
下一頁 exercise4401.py
回 Python 教材首頁
回程式語言教材首頁