主框架
import tkinter
# 创建主窗口
win = tkinter.Tk()
# 设置标题
win.title('标题')
# 设置大小和位置
win.geometry('400x400+200+20')
# 进入消息循环
win.mainloop()
label
import tkinter
win = tkinter.Tk()
win.title('sunk')
win.geometry('400x400+200+20')
# 进入消息循环,Label:标签控件可以显示文本
# win 主窗口
# text 显示文本内容
# bg 背景色
# fg 字体颜色
# font 字体,大小
# width
# height
# wraplength 指定text文本中多宽进行换行
#justify 设置换行后的对齐方式
# anchor 位置 n e s w c ne se ..
w= tkinter.Label(win,text='sunck is a good man',bg='blue',fg='red',
font=('黑体',20),width=100,height=10,wraplength=30,justify='left',
anchor='n')
# 显示出来
w.pack()
win.mainloop()
button
import tkinter
win = tkinter.Tk()
win.title('标题')
win.geometry('400x400+200+20')
# 创建按钮
# button = tkinter.Button(win,text='按钮',command=func,width=10,
# height=10)
# button.pack()
button1 = tkinter.Button(win,text='退出',command=win.quit,width=10,
height=10)
button1.pack()
win.mainloop()
entry
import tkinter
win = tkinter.Tk()
win.title('标题')
win.geometry('400x400+200+20')
**# 输入控件,用于显示简单的文本内容
#show 密文显示
#绑定变量
e=tkinter.Variable()
#entry = tkinter.Entry(win,show='*')
entry = tkinter.Entry(win,textvariable=e)
#e就代表输入框这个对象
#设置值
e.set('sunck is a good man')
entry.pack()
#取值
print(e.get())
print(entry.get())
win.mainloop()
text
import tkinter
win = tkinter.Tk()
win.title('标题')
win.geometry('400x400+200+20')
#文本控件,用于显示多行文本
# height 表示显示的行数
# 创建滚动条
scroll=tkinter.Scrollbar()
text = tkinter.Text(win,width=80,height=4)
# side放到窗体的那一侧,fill填充
scroll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
text.pack(side=tkinter.LEFT,fill=tkinter.Y)
str1='nidfhewufhwif'
text.insert(tkinter.INSERT,str1)
#关联,让滚动条能上下移动
scroll.config(command=text.yview)
text.config(yscrollcommand=scroll.set)
win.mainloop()
Checkbutton
import tkinter
win = tkinter.Tk()
win.title('标题')
win.geometry('400x400+200+20')
def updata():
message =''
if hobby1.get() == True:
message +='money\n'
if hobby2.get() == True:
message +='power\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=updata)
check1.pack()
hobby2=tkinter.BooleanVar()
check2 = tkinter.Checkbutton(win,text='power',variable=hobby2,
command=updata)
check2.pack()
check3 = tkinter.Checkbutton(win,text='person')
check3.pack()
text=tkinter.Text(win,width=50,height=5)
text.pack()
win.mainloop()
radiobutton
from tkinter import *
win =Tk()
win.title('单选框')
win.geometry('400x400+200+200')
def updata():
print(r.get())
r= IntVar()
radio1 = Radiobutton(win,text='one',value=1,variable=r,command=updata)
radio1.pack()
radio2 = Radiobutton(win,text='two',value=2,variable=r,command=updata)
radio2.pack()
win.mainloop()