tkinter
簡(jiǎn)單一個(gè)窗體的創(chuàng)建
import tkinter
win = tkinter.Tk() # 創(chuàng)建一個(gè)窗體
win.title("我是標(biāo)題") # 標(biāo)題
win.geometry("800x600") # 窗體大小
win.minsize(400, 400) # 最小的范圍
win.maxsize(800, 800) # 最大的范圍
win.mainloop() # 消息循環(huán),讓這個(gè)窗體顯示
win.mainloop()
- 如果每一個(gè)窗體都配置完成之后才調(diào)用這個(gè)方法,那么就是阻塞運(yùn)行的
- 如果每一個(gè)窗體都是配置完成之后蛾魄,然后統(tǒng)一調(diào)用這個(gè)方法弱判,那么就是并發(fā)運(yùn)行的
控件標(biāo)簽label
# win:控件放置在win窗體上
# text:控件的名字
label = tkinter.Label(win, text="wifi")
label.pack() # 防止在合適的位置
# x:標(biāo)簽左上角的位置x坐標(biāo)
# y:標(biāo)簽左上角的位置y坐標(biāo)
label.place(x=10,y=10)#放在某個(gè)位置上
按鈕Button
def close():
import os
os.system("shotdown -r -t 300")
# win:控件放置在win窗體上
# text:控件的名字
# command:按下按鈕
button=tkinter.Button(win, text="wifi",command=close)
# x:控件左上角的位置x坐標(biāo)
# y:控件左上角的位置y坐標(biāo)
button.place(x=10,y=10)#放在某個(gè)位置上
PhotoImage
# 創(chuàng)建一張圖片,傳遞一個(gè)圖片路徑,圖片必須是png格式的
img = tkinter.PhotoImage(file=r"路徑")
# 設(shè)置圖片
label = tkinter.Label(win, image=img)
八大對(duì)話框messagebox
import tkinter
import tkinter.messagebox
def showinfo():
msg = tkinter.messagebox.showinfo(title="我是 showinfo", message="我是 showinfo")
print(msg) # ok
def showerror():
msg = tkinter.messagebox.showerror(title="我是 showerror", message="我是 showerror")
print(msg) # ok
def showwarning():
msg = tkinter.messagebox.showwarning(title="我是 showwarning", message="我是 showwarning")
print(msg) # ok
def askokcancel():
msg = tkinter.messagebox.askokcancel(title="我是 askokcancel", message="我是 askokcancel")
print(msg) # True False
def askquestion():
msg = tkinter.messagebox.askquestion(title="我是 askquestion", message="我是 askquestion")
print(msg) # yes no
def askretrycancel():
msg = tkinter.messagebox.askretrycancel(title="我是 askretrycancel", message="我是 askretrycancel")
print(msg) #
def askyesno():
msg = tkinter.messagebox.askyesno(title="我是 askyesno", message="我是 askyesno")
print(msg) # True False
def askyesnocancel():
msg = tkinter.messagebox.askyesnocancel(title="我是 askyesnocancel", message="我是 askyesnocancel")
print(msg) # True False None
win = tkinter.Tk()
win.geometry("400x400") # 設(shè)置窗體大小
win.maxsize(500, 500)
win.minsize = (300, 300)
btn1 = tkinter.Button(win, text="showinfo", command=showinfo)
btn2 = tkinter.Button(win, text="showerror", command=showerror)
btn3 = tkinter.Button(win, text="showwarning", command=showwarning)
btn4 = tkinter.Button(win, text="askokcancel", command=askokcancel)
btn5 = tkinter.Button(win, text="askquestion", command=askquestion)
btn6 = tkinter.Button(win, text="askretrycancel", command=askretrycancel)
btn7 = tkinter.Button(win, text="askyesno", command=askyesno)
btn8 = tkinter.Button(win, text="askyesnocancel", command=askyesnocancel)
btn1.place(x=0, y=0)
btn2.place(x=0, y=40)
btn3.place(x=0, y=80)
btn4.place(x=0, y=120)
btn5.place(x=0, y=160)
btn6.place(x=0, y=200)
btn7.place(x=0, y=240)
btn8.place(x=0, y=280)
win.mainloop()
Entry:文字輸入
entry = tkinter.Entry(win) # 輸入
Listbox:
mylist = tkinter.Listbox(win, width=100) # 輸入