00基礎代碼
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
win.mainloop()
01簡單示例
#建立主視窗
win = tkinter.Tk()
#設定标題
win.title("Liuwang")
#設定大小和位置
win.geometry("400x400+200+20")
#進入消息循環
win.mainloop()
02Label控件
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
'''
Label:标簽控件
可以顯示文本
'''
#win:父窗體
#bg:背景色
#fg :字型顔色
#text:文本内容
# wraplength:制定text文本中多款進行換行
#justify :設定換行後的對齊方式
#anchor :位置 n北 e東 s南 w西 center居中 ne東北.....
label = tkinter.Label(win,
text = "LIUwang",
bg = "blue",
fg = "red",
font = ("黑體",20),
width =10 ,
height=10,
wraplength =100,
justify ="left",
anchor = "center"
)
#顯示出來
label.pack()
win.mainloop()
03Button控件
import tkinter
def func():
print("Liuwang is handesome")
win = tkinter.Tk()
#設定标題
win.title("Liuwang")
#設定大小和位置
win.geometry("400x400+200+20")
#進入消息循環
button1 = tkinter.Button(win,text="按鈕",command= func,
width =10,height = 10 )
button1.pack()
button2 = tkinter.Button(win,text="按鈕",command = win.quit)
button2.pack()
win.mainloop()
04entry控件
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
'''
輸入控件
用于顯示簡單的文本内容
'''
#show="*"密文顯示
#綁定變量
e = tkinter.Variable()
entry = tkinter.Entry(win,textvariable= e)
entry.pack()
#e就代表輸入框這個對象
e.set("Liuwang is handsome")
print(e.get())
print(entry.get())
win.mainloop()
05點選按鈕輸入框内容
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
def showInfo():
print(entry.get())
entry = tkinter.Entry(win)
entry.pack()
button = tkinter.Button(win,text="按鈕",command= showInfo)
button.pack()
win.mainloop()
06Text控件
import tkinter
#建立主視窗
win = tkinter.Tk()
#設定标題
win.title("Liuwang")
#設定大小和位置
win.geometry("400x400+200+20")
#進入消息循環
'''
文本控件,用于顯示多行文本
'''
#height :顯示行數
text = tkinter.Text(win,width = 30 ,height = 4 ,)
text.pack()
str ="簡介:印度阿三,指印度人,帶有種族歧視意味的貶義稱呼,阿SIR音譯。“印度阿三”來自“十裡洋場”時期的吳語上海話,吳人極喜加“阿”字,而上海話中與“三”相關的詞彙(阿三、八三、癟三、十三點、豬頭三)多為貶義詞。上海當年的英租界中經常會有從印度調來的“公務員”,負責一些雜事,而這些印度人"
text.insert(tkinter.INSERT,str)
win.mainloop()
07帶滾動條的Text
import tkinter
#建立主視窗
win = tkinter.Tk()
#設定标題
win.title("Liuwang")
#設定大小和位置
win.geometry("400x400+200+20")
#進入消息循環
'''
文本控件,用于顯示多行文本
'''
#建立滾動條
scroll = tkinter.Scrollbar()
#height :顯示行數
text = tkinter.Text(win,width = 50 ,height = 8 ,)
#side放到窗體的哪一側
scroll.pack(side = tkinter.RIGHT,fill = tkinter.Y)
text.pack(side = tkinter.LEFT,fill = tkinter.Y)
#關聯
scroll.config(command = text.yview)
text.config(yscrollcommand = scroll.set)
str ="簡介:印度阿三,指印度人,帶有種族歧視意味的貶義稱呼,阿SIR音譯。“印度阿三”來自“十裡洋場”時期的吳語上海話,吳人極喜加“阿”字,而上海話中與“三”相關的詞彙(阿三、八三、癟三、十三點、豬頭三)多為貶義詞。上海當年的英租界中經常會有從印度調來的“公務員”,負責一些雜事,而這些印度人"
text.insert(tkinter.INSERT,str)
win.mainloop()
08CheckButton多選框控件
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
def update():
message=""
if hobby1.get()==True:
message += "money\n"
if hobby2.get()==True:
message += "power\n"
if hobby3.get()==True:
message += "people\n"
#清除text中的所有内容
text.delete(0.0,tkinter.END)
text.insert(tkinter.INSERT,message)
# 要綁定的變量
hobby1 = tkinter.BooleanVar()
#多選框
check1 = tkinter.Checkbutton(win,text="money",
variable= hobby1,command = update)
check1.pack()
hobby2 = tkinter.BooleanVar()
check2 = tkinter.Checkbutton(win,text="power",
variable=hobby2, command=update)
check2.pack()
hobby3 = tkinter.BooleanVar()
check3 = tkinter.Checkbutton(win,text="people",
variable = hobby3, command = update)
check3.pack()
text = tkinter.Text(win,height= 5,width = 50)
text.pack()
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
def update():
print(r.get())
#一組單選框要綁定同一個變量
r= tkinter.IntVar() #StringVar()
radio1 = tkinter.Radiobutton(win,text="one",value = 1,
variable= r,command= update)
radio1.pack()
radio2 = tkinter.Radiobutton(win,text="two",value = 2,
variable= r,command= update)
radio2.pack()
win.mainloop()