Skip to content

Commit 2bc02e4

Browse files
committed
add ch7
1 parent 6417d05 commit 2bc02e4

9 files changed

+231
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
2. [API](https://hackmd.io/@macs1207/HJF56PlnS#/3)
2626
- [CH6 Python[5]](https://hackmd.io/@macs1207/BkU_hwqnS#)
2727
1. [Flask](https://hackmd.io/@macs1207/BkU_hwqnS#/2)
28+
- [CH7 Python[6]](https://hackmd.io/@macs1207/HJCrrO76r#)
29+
1. [Tkinter](https://hackmd.io/@macs1207/HJCrrO76r#/2)
2830

2931

3032
# 作者

ch7/calculator.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from tkinter import *
2+
3+
4+
class Caculator:
5+
def __init__(self, root):
6+
self.root = root
7+
self.result = 0
8+
inputEntry = Entry(root)
9+
inputEntry.pack(padx=10, pady=10, fill=X)
10+
caculateButton = Button(root, text="Caculate", width=10,
11+
height=1, font=["Arial", 22], command=lambda: self.caculate(inputEntry.get(), resultVar))
12+
caculateButton.pack(padx=10, pady=10, fill=X)
13+
resultVar = IntVar()
14+
resultLabel = Label(root, textvariable=resultVar,
15+
font=["Arial", 22])
16+
resultLabel.pack(padx=10, pady=10, fill=X)
17+
18+
def caculate(self, input, result):
19+
try:
20+
result.set(eval(input))
21+
except SyntaxError:
22+
result.set("Invalid Input.")
23+
24+
25+
def main():
26+
root = Tk()
27+
root.title("計算機")
28+
Caculator(root)
29+
root.mainloop()
30+
31+
32+
if __name__ == "__main__":
33+
main()

ch7/horse_racing.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from tkinter import *
2+
import random
3+
4+
# class init
5+
6+
7+
def gui_init():
8+
root.title("Horse Racing")
9+
screeenWidth = root.winfo_screenwidth()
10+
screeenHeight = root.winfo_screenheight()
11+
w = 400
12+
h = 300
13+
root.geometry("{}x{}+{}+{}".format(w, h, (screeenWidth - w) //
14+
2, (screeenHeight - h) // 2))
15+
16+
17+
def goalGenerate():
18+
goalLbl = []
19+
for i in range(8):
20+
goalLbl.append(Label(root, text="|", font=("Consolas", 16)))
21+
goalLbl[i].place(x=385, y=0 + i * 30)
22+
23+
24+
def horseGenerate():
25+
horseLbl = []
26+
for i in range(8):
27+
horseLbl.append(
28+
Label(root, text="~/-\^", font=("Consolas", 16), relief="raised"))
29+
horseLbl[i].place(x=0, y=0 + i * 30)
30+
return horseLbl
31+
32+
33+
def horseMove(horse):
34+
location = [0] * 8
35+
36+
def move():
37+
idx = random.randrange(8)
38+
location[idx] += 10
39+
for i in range(8):
40+
horse[i].place(x=location[i], y=0 + i * 30)
41+
if(location[idx] < 330):
42+
root.after(10, move)
43+
else:
44+
endMsg = Label(root, text="Horse " + str(idx + 1) +
45+
" wins!", font=("Consolas", 16))
46+
endMsg.place(y=260)
47+
move()
48+
49+
50+
root = Tk()
51+
gui_init()
52+
goalGenerate()
53+
horseLbl = horseGenerate()
54+
horseMove(horseLbl)
55+
root.mainloop()

ch7/tk_basic.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import tkinter
2+
3+
root = tkinter.Tk()
4+
# 進入mainloop
5+
root.mainloop()

ch7/tk_button.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import tkinter
2+
3+
on_hit = False
4+
5+
6+
def hit_me():
7+
global on_hit
8+
if on_hit:
9+
on_hit = False
10+
var.set('12/17有活動')
11+
else:
12+
on_hit = True
13+
var.set('12/26也有')
14+
15+
16+
# 初始化
17+
root = tkinter.Tk()
18+
root.title('109-NKUST Information Technology Club')
19+
root.geometry('480x360')
20+
21+
# 用來存 label 的文字
22+
var = tkinter.StringVar()
23+
var.set('12/17有活動')
24+
# textvariable 參數用來指定 StringVar 物件
25+
lable = tkinter.Label(root, textvariable=var, bg='white',
26+
font=('Arial', 12), width=30, height=2)
27+
lable.place(relx=0.5, rely=0.5, anchor='center')
28+
b = tkinter.Button(root, text='點一下', width=15,
29+
height=2, command=hit_me).pack()
30+
31+
root.mainloop()

ch7/tk_entry.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import tkinter
2+
3+
def hit_me():
4+
var = e.get()
5+
# 可以把 insert 換成 end 試試看
6+
t.insert('insert', var)
7+
8+
root = tkinter.Tk()
9+
root.title('109-NKUST Information Technology Club')
10+
root.geometry('480x360')
11+
e = tkinter.Entry(root)
12+
e.pack()
13+
b = tkinter.Button(root, text='點一下insert', width=15, height=2, command=hit_me)
14+
b.pack()
15+
t = tkinter.Text(root, height=5)
16+
t.pack()
17+
root.mainloop()

ch7/tk_grid.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from tkinter import *
2+
3+
class Main:
4+
def __init__(self, root):
5+
self.root = root
6+
7+
def main(self):
8+
buttons = []
9+
10+
# Button 0
11+
button = Button(self.root, text=0, width=30, height=5)
12+
buttons.append(button)
13+
14+
# Button 1~9
15+
for i in range(1, 10):
16+
button = Button(self.root, text=str(i), width=10, height=5)
17+
buttons.append(button)
18+
19+
for row in range(3):
20+
for column in range(3):
21+
index = row * 3 + column + 1
22+
buttons[index].grid(row=row, column=column)
23+
buttons[0].grid(row=3, column=0, columnspan=3)
24+
25+
def main():
26+
root = Tk()
27+
28+
# 視窗標題
29+
title = "標題"
30+
31+
screenWidth = root.winfo_screenwidth()
32+
screenHeight = root.winfo_screenheight()
33+
root.title(title)
34+
Main(root).main()
35+
root.mainloop()
36+
37+
if __name__ == "__main__":
38+
main()

ch7/tk_label.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import tkinter
2+
root = tkinter.Tk()
3+
4+
#視窗名稱
5+
root.title('109-NKUST Information Technology Club')
6+
7+
#視窗大小
8+
root.geometry('480x360')
9+
10+
# text:要顯示的文字
11+
# bg:背景顏色
12+
# font:自型設定
13+
# width, height:寬, 高
14+
lable = tkinter.Label(root, text='12/17有活動', bg='white',
15+
font=('Arial', 12), width=30, height=2)
16+
17+
lable.place(relx=0.5, rely=0.5, anchor='center')
18+
root.mainloop()

ch7/tk_template.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from tkinter import *
2+
3+
class Main:
4+
def __init__(self, root):
5+
self.root = root
6+
7+
def main(self):
8+
# 新增元件及排版可以寫在這裡
9+
pass
10+
11+
def main():
12+
root = Tk()
13+
14+
# 視窗寬度
15+
width = 256
16+
17+
# 視窗高度
18+
height = 256
19+
20+
# 視窗標題
21+
title = "標題"
22+
23+
screenWidth = root.winfo_screenwidth()
24+
screenHeight = root.winfo_screenheight()
25+
root.geometry("{}x{}+{}+{}".format(width, height,
26+
(screenWidth - width) // 2, (screenHeight - height) // 2))
27+
root.title(title)
28+
Main(root).main()
29+
root.mainloop()
30+
31+
if __name__ == "__main__":
32+
main()

0 commit comments

Comments
 (0)