---
# System prepended metadata

title: 和數字有關的widget
tags: [活用tkinter – 學習運用GUI]

---

# 和數字有關的widget
###### tags: `活用tkinter – 學習運用GUI`

## Scale的數值輸入控制
* 數值捲軸，先製作水平與垂直的卷軸......
```python=
from tkinter import *

root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.config(bg = "lightgreen")

scale_1 = Scale(root, from_= 0, to = 10).pack()
scale_2 = Scale(root, from_ = 0, to = 10,
            length = 300, orient = HORIZONTAL).pack()

root.mainloop()
```
* 結果
![](https://i.imgur.com/u0hnX3K.png)


## 取得卷軸值
```python=
def pinfo():
    print(scale_1.get())

scale_1 = Scale(root, from_= 0, to = 10)
scale_1.set(3)
scale_1.pack()

Button(root, text = "數值", command = pinfo).pack()
```

* 結果
![](https://i.imgur.com/7xEg4pq.png)

## askcolor方法
```python=
def pinfo():
    mycolor = askcolor()
    root.config(bg = mycolor[1])
    
Button(root, text = "數值", command = pinfo).pack(pady = 20)
```
* 結果
![](https://i.imgur.com/sMCcjeb.png)

## spinbox控件
* 最簡單的spinbox
```python=
spin = Spinbox(root, from_ = 0, to = 10, increment = 2)
# increment(間隔)
spin.pack(pady = 20)
```
* 結果
![](https://i.imgur.com/KfD1E9h.png)

* get()函數
```python=
def pinfo():
    print(scale_1.get())
```

* 使用序列儲存數值
```python=
scale_1 = Spinbox(root, value = (5, 10, 20 ,30), command = pinfo)
```

* 如果是非數值資料
```python=
scale_1 = Spinbox(root, value = ("r", "e", "d"), command = pinfo)
```

:::warning
:+1: 這是一個挺方便的道具~~
:::


{%hackmd S1DMFioCO %}