# NKUST ITC 108-1 Python[6]
###### [https://python-slide.macs1207.dev](https://python-slide.macs1207.dev)
###### 2019/12/03
###### tags: `NKUST ITC` `Python 社課`
[TOC]
---
## [Github](https://github.com/macs1207/python-tutorial)
----
## 剩下的時間安排
|日期|內容|地點
|-|-|-|
|12/03|Tkinter|資1109|
|12/17|講座|資1109
|12/26|期末社員大會||
---
## Tkinter
[DOCS](https://docs.python.org/3/library/tkinter.html)
[Tutorial](https://www.tutorialspoint.com/python/python_gui_programming.htm)
----
### 基本元件
|控制元件|功能|
|:----:|:------|
|button|按鈕控制元件;在程式中顯示按鈕|
|label|標簽控制元件;可以顯示文字和點陣圖|
|entry|輸入控制元件;用於顯示簡單的文字內容|
|text|文字控制元件;用於顯示多行文字|
----
|控制元件|功能|
|:----:|:------|
|canvas|畫布控制元件;顯示圖形元素如線條或文字|
|checkbutton|多選框控制元件;用於在程式中提供多項選擇框|
|frame|框架控制元件;在螢幕上顯示一個矩形區域,多用來作為容器|
----
### 標準屬性
|屬性|功能|
|:----:|:------|
|Dimension|控制元件大小|
|Color|控制元件顏色|
|Font|控制元件字型|
|Anchor|錨點|
|Relief|控制元件樣式|
|Bitmap|點陣圖|
|Cursor|游標|
----
### 定位處理
|方法|用途|
|:----:|:------|
|pack()|包裝|
|grid()|表格|
|place()|定位|
---
### 最基本的
```python=
import tkinter
root = tkinter.Tk()
# 進入mainloop
root.mainloop()
```
執行結果

----
## Label
### 標籤
- 顯示單行文字
----
```python=
import tkinter
root = tkinter.Tk()
#視窗名稱
root.title('109-NKUST Information Technology Club')
#視窗大小
root.geometry('480x360')
# text:要顯示的文字
# bg:背景顏色
# font:自型設定
# width, height:寬, 高
label = tkinter.Label(root, text='12/17有活動', bg='white',
font=('Arial', 12), width=30, height=2)
label.place(relx=0.5, rely=0.5, anchor='center')
root.mainloop()
```
----
執行結果

----
## Button
### 按鈕
----
```python=
import tkinter
on_hit = False
def hit_me():
global on_hit
if on_hit:
on_hit = False
var.set('12/17有活動')
else:
on_hit = True
var.set('12/26也有')
# 初始化
root = tkinter.Tk()
root.title('109-NKUST Information Technology Club')
root.geometry('480x360')
# 用來存 label 的文字
var = tkinter.StringVar()
var.set('12/17有活動')
# textvariable 參數用來指定 StringVar 物件
label = tkinter.Label(root, textvariable=var, bg='white',
font=('Arial', 12), width=30, height=2)
label.place(relx=0.5, rely=0.5, anchor='center')
b = tkinter.Button(root, text='點一下', width=15,
height=2, command=hit_me).pack()
root.mainloop()
```
----
執行結果

----

----
## Entry
### 文字方塊
----
```python=
import tkinter
def hit_me():
var = e.get()
# 可以把 insert 換成 end 試試看
t.insert('insert', var)
root = tkinter.Tk()
root.title('109-NKUST Information Technology Club')
root.geometry('480x360')
e = tkinter.Entry(root)
e.pack()
b = tkinter.Button(root, text='點一下insert', width=15, height=2, command=hit_me)
b.pack()
t = tkinter.Text(root, height=5)
t.pack()
root.mainloop()
```
----
執行結果

----
## 在練習之前
### 給大家一個標準模板(?
```python=
from tkinter import *
class Main:
def __init__(self, root):
self.root = root
def main(self):
# 新增元件及排版可以寫在這裡
pass
def main():
root = Tk()
# 視窗寬度
width = 256
# 視窗高度
height = 256
# 視窗標題
title = "標題"
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
root.geometry("{}x{}+{}+{}".format(width, height,
(screenWidth - width) // 2, (screenHeight - height) // 2))
root.title(title)
Main(root).main()
root.mainloop()
if __name__ == "__main__":
main()
```
----
- 練習一
- 寫一個計算機
- 試試 eval()
- [範例](https://github.com/macs1207/python-tutorial/blob/master/ch7/calculator.py)
- 練習二
- 寫一個有按鍵的計算機
- 建議用grid排版
- [按鍵範例](https://github.com/macs1207/python-tutorial/blob/master/ch7/tk_grid.py)
- 練習三
- 賽馬(???
- [範例](https://github.com/macs1207/python-tutorial/blob/master/ch7/horse_racing.py)
---
## Discord
- [https://discord.gg/hmUeXeH](https://discord.gg/hmUeXeH)

----
## Telegram
- [https://t.me/kuasitc](https://t.me/kuasitc)
- 
----
## 作者
- 社長 [Macs](https://github.com/macs1207)
- 副社長 [呆呆大蛙](https://github.com/daidaidarwa)
----
## 參考資料
- [Python-100-Days](https://github.com/jackfrued/Python-100-Days)
- [Wikipedia](https://zh.wikipedia.org)
- [成大X-Village教材](https://www.facebook.com/pages/category/Education/X-Village-423736361424301)
- [Python圖形使用者介面-Tkinter](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/639870/)
- [Python GUI 程式設計(Tkinter)](https://www.itread01.com/study/python-gui-tkinter.html)
{"metaMigratedAt":"2023-06-15T02:12:12.129Z","metaMigratedFrom":"Content","title":"NKUST ITC 108-1 Python[6]","breaks":true,"contributors":"[{\"id\":\"8a9f2da6-22f2-4329-8d35-37824be8c99d\",\"add\":3733,\"del\":693},{\"id\":\"0543727d-0e35-443a-a198-84223cf6d534\",\"add\":3871,\"del\":2342}]"}