- 简单示例
import tkinter
#创建主窗口
win = tkinter.Tk()
#设置标题
win.title("xinghaohan")
#设置大小和位置
win.geometry("400x400+200+20")
#进入消息循环
win.mainloop()
- Lable控件
import tkinter
win = tkinter.Tk()
win.title("xinghaohan")
win.geometry("400x400+200+20")
#进入消息循环
'''
Lable:标签控件,可以显示文本
'''
#win:父窗体
#text:显示的文本内容
#bg 背景色
#fg 字体颜色
#wraplength 指定text文本中多宽进行换行
#justify 设置换行后的对齐方式
#anchor 位置 n北 e东 s南 w西 center居中 ne se nw sw
lable = tkinter.Label(win, text = "a good man", bg = "pink", fg = "red",
font = ("黑体", 20), width = 10, height = 10,
wraplength = 30, justify = "left", anchor = "e")
#显示出来
lable.pack()
win.mainloop()
- Button控件
import tkinter
def func():
print("good good study")
win = tkinter.Tk()
win.title("xinghaohan")
win.geometry("400x400+200+20")
#创建按钮
button1 = tkinter.Button(win, text = "按钮", command = func, width=10,
height=10)
#显示按钮
button1.pack()
button2 = tkinter.Button(win, text = "按钮", command = lambda:print("day day up"))
#显示按钮
button2.pack()
button3 = tkinter.Button(win, text = "按钮", command = win.quit)
#显示按钮
button3.pack()
win.mainloop()
- Entry控件
import tkinter
win = tkinter.Tk()
win.title("xinghaohan")
win.geometry("400x400+200+20")
'''
输入控件
用于显示简单的文本内容
'''
#绑定变量
e = tkinter.Variable()
#show 密文显示, show="*"
entry = tkinter.Entry(win, textvariable=e)
entry.pack()
#e就代表输入框这个对象
#设置值
e.set("good good study")
#取值
print(e.get())
print(entry.get())
win.mainloop()
- 点击按钮输出输入框中的内容
import tkinter
win = tkinter.Tk()
win.title("xinghaohan")
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()
- Text控件
import tkinter
win = tkinter.Tk()
win.title("xinghaohan")
win.geometry("400x400+200+20")
'''
文本控件,用于显示多行文本
'''
#height 显示的行数
text = tkinter.Text(win, width=30, height=4)
text.pack()
str = '''d f gfzxcvbdshv hdfhvrj dfbjsbfdbfdhbfyeiqoe
jalja,z skljfiweu usfdjknmzxnfsuefsdjfn djkfowpqwa[jdkjnzjnmdfjskfnjksfieklj
iso kdjnfis jklfio jfho
dfjdfie jdfhiuweujiioop ppppp kkkkk k mjkjjjkl l;;;;ksjdjjsjdjskj'''
text.insert(tkinter.INSERT, str)
win.mainloop()
- 带滚动条的Text控件
import tkinter
win = tkinter.Tk()
win.title("xinghaohan")
#win.geometry("400x400+200+20")
'''
文本控件,用于显示多行文本
'''
#创建滚动条
scroll = tkinter.Scrollbar()
text = tkinter.Text(win, width=30, height=4)
#side放到窗体的哪一侧 fill填充
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 = '''d f gfzxcvbdshv hdfhvrj dfbjsbfdbfdhbfyeiqoejalja,z skljfiweu usfdjknmzxnfsue
fsdjfn djkfowpqwa[jdkjnzjnmdfjsk
fdsf
sdfdsf
dfsdfd
sfdsf
fnjksfiekljiso kdjnfis jklfio jfho
dfjdfie jdfhiuweujiioop ppppp kkkkk k mjkjjjkl l;;;;ksjdjjsjdjskj'''
text.insert(tkinter.INSERT, str)
win.mainloop()