xia的小窩

一起來coding和碼字吧

0%

和數字有關的widget

Scale的數值輸入控制

數值捲軸,先製作水平與垂直的卷軸……

1
2
3
4
5
6
7
8
9
10
11
12
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()

結果

取得卷軸值

1
2
3
4
5
6
7
8
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()

結果

askcolor方法

1
2
3
4
5
def pinfo():
mycolor = askcolor()
root.config(bg = mycolor[1])

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

結果

spinbox控件

最簡單的spinbox

1
2
3
spin = Spinbox(root, from_ = 0, to = 10, increment = 2)
# increment(間隔)
spin.pack(pady = 20)

結果

get()函數

1
2
def pinfo():
print(scale_1.get())

使用序列儲存數值

1
scale_1 = Spinbox(root, value = (5, 10, 20 ,30), command = pinfo)

如果是非數值資料

1
scale_1 = Spinbox(root, value = ("r", "e", "d"), command = pinfo)