-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfordemo.py
45 lines (36 loc) · 1.23 KB
/
fordemo.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
import tkinter as tk
class ForDemo(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.winfo_toplevel().title("計算總和")
self.label = tk.Label(self)
self.label["text"] = "請輸入整數:"
self.label.grid(row=0, column=0, sticky=tk.N+tk.W)
self.entry = tk.Entry(self)
self.entry.grid(row=1, column=0, sticky=tk.N+tk.W)
self.button = tk.Button(self)
self.button["text"] = "計算"
self.button["command"] = self.calculate
self.button.grid(row=2, column=0, sticky=tk.N+tk.W)
self.result = tk.Label(self)
self.result["text"] = "結果是..."
self.result.grid(row=3, column=0, sticky=tk.N+tk.W)
def calculate(self):
number = int(self.entry.get())
sum = 0
for i in range(number + 1):
sum += i
else:
self.result["text"] = str(sum)
if __name__ == '__main__':
root = tk.Tk()
app = ForDemo(master=root)
app.mainloop()
#《程式語言教學誌》的範例程式
# http://kaiching.org/
# 檔名:fordemo.py
# 功能:示範 for-in-else 陳述
# 作者:張凱慶