## 20240224 python 筆記
### tkinter:check box
IntVar():創建一個整數變數
booleanVar():創建boolean變數(0 or 1)-->0nvalue改成1,offvalue改成0
StringVar():--->onvalue/offvalue改成"True"/"False"
但都需要get()
```python
from tkinter import*
def show_result():
if x.get()==1: #要get()取得值
print("i agree")
else:
print("no")
window=Tk()
window.geometry("200x200")
x=IntVar() #建立一個整數變數
button=Checkbutton(window,
text="agree",
variable=x,
onvalue=1, #checkbox 被選取時給x的值
offvalue=0, #checkbox 被取消選取時給x的值
command=show_result
#image=image_name #加入icon
#compound=right/left #checkbox在icon的右/左邊
)
button.pack()
window.mainloop()
```
### radio_button
like checkbox but there are many choices.we can choose one of the group.
```python
from tkinter import *
food=["apple","fish","meat"] #要將每一格選項都有一個button
def show():
print(x.get())
window=Tk()
x=IntVar()
for i in range(len(food)):
radio_button=Radiobutton(window,
text=food[i],
variable=x, #value會把值傳給variable,而3個button共用一格variable
value=i, #每一個選項都賦予不同的value才有辦法選擇
indicatoron=0, #取消圓圈button,為選擇文字框
command=show)
radio_button.pack()
window.mainloop()
```
### sliding_scale:
```python
from tkinter import*
window=Tk()
def submit():
print(sliding_scale.get()) #取得sliding sacle裡的value
sliding_scale=Scale(window, #create a sliding scale
from_=100,
to=0,
length=600, #the size of sliding_scale
orient=HORIZONTAL, #orientation of scale
tickinterval=20, #the interval of the scale
showvalue=0,
troughcolor="blue")
sliding_scale.pack()
sliding_scale.set(50) #set initial value
button=Button(window,text="submit",command=submit)
button.pack()
window.mainloop()
```
### listbox
a list of selectable text items in the container
```python
from tkinter import*
def submit():
print(listbox.get(listbox.curselection())) #listbox.curseletion() 回傳選定的"index",再用get(index)取其值
# color=[]
# for i in listbox.curselection():
# color.insert(i,listbox.get(i)) #multiple selectiono
# print(color)
def add():
listbox.insert(listbox.size(),entry.get()) #insert(index,value),插入在entrybox中輸入的內容
listbox.config(height=listbox.size()) #新增後更新listbox大小
def delete():
listbox.delete(listbox.curselection()) #listbox.delete(index)
listbox.config(height=listbox.size())
window=Tk()
listbox=Listbox(window, #create a listbox
background="#f7ffde",
font=("Constantia",20),
#selectmode=MULTIPLE #一次選擇多格
)
listbox.insert(0,"red") #在listbox中插入新選項(index,value)
listbox.insert(1,"green")
listbox.insert(2,"blue")
listbox.insert(3,"yellow")
entry=Entry(window) #create a entrybox
submit_button=Button(window,text="submit",command=submit)
add_button=Button(window,text="add",command=add)
delete_button=Button(window,text="delete",command=delete)
listbox.config(height=listbox.size())
listbox.pack()
submit_button.pack()
add_button.pack()
delete_button.pack()
entry.pack()
window.mainloop()
```
### messagebox
創建彈出視窗
純視窗(只包含確定按鈕)
messagebox.showinfo(title="",message="")
messagebox.showwarning(title="",message="")
messagebox.showerror(title="",message="")
ask(多選項):會回傳true/false
messagebox.askokcancel(title="",message="")
messagebox.askretrycancel(titile="",message="")
messagebox.askyesno(title="",message="")
messagebox.askyesnocancel(title="",message="")
回傳"yes"/"no"
messagebox.askquestion(title="",message="")
```python
from tkinter import *
from tkinter import messagebox
def click():
#messagebox.showinfo(title="this is a messagebox",message="hello")
# if messagebox.askokcancel(title="",message="do u want it?") == True:
# print("yes")
# else:
# print("no")
answer=messagebox.askquestion(title="",message="what")
if answer=="yes": #askquestion 為yes!!!
print("true")
else:
print("n")
screen=Tk()
button=Button(screen,text="click",command=click)
button.pack()
screen.mainloop()
```