import tkinter as tk
app = tk.Tk() # 实例化Tk
app.title("Demo") # 设置标题
theLabel = tk.Label(app, text="我的第二个窗口程序!") # 显示文本
theLabel.pack() # 自动调节尺寸
app.mainloop() # 窗口的主事件循环,最后一句
import tkinter as tk
class APP:
"""封装成类"""
def __init__(self, master):
frame = tk.Frame(master)
# 设置在左边,x轴间距10,Y轴一样
frame.pack(side=tk.LEFT, padx=10, pady=10)
# 设置按钮,“打招呼”,蓝色按钮字体,黑色背景
self.hi_there = tk.Button(frame, text="打招呼", fg="blue",
command=self.say_hi, bg="black")
self.hi_there.pack() # 自动调节尺寸
def say_hi(self):
print("搭嘎好")
root = tk.Tk() # Tk实例
app = APP(root) # 类实例
root.mainloop() # 结尾语句
from tkinter import * # 导入Tk里面的所有
root = Tk() # 创建实例
# 左边的字
textLabel = Label(root,
text="胡珍珍\n打架吗?",
justify=LEFT,
padx=10)
textLabel.pack(side=LEFT)
# 右边的图片
photo = PhotoImage(file="18.gif")
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)
mainloop()
from tkinter import * # 导入Tk里面的所有
root = Tk()
photo = PhotoImage(file="18.gif")
theLabel = Label(root, text="猪",
justify=LEFT,
image=photo,
compound=CENTER,
font="楷体, 20",
fg="red")
theLabel.pack()
mainloop()
frame控件是将窗口分成好几个小模块,然后每个小模块中添加控件。
也就是将窗口合理的布局
from tkinter import * # 导入Tk里面的所有
def callback():
var.set("谁怕谁")
root = Tk()
frame1 = Frame(root)
frame2 = Frame(root)
var = StringVar() # 创建实例
# 左边的字,frame1
var.set("小黑黑\n打架吗?")
textLabel = Label(frame1,
textvariable=var,
justify=LEFT)
textLabel.pack(side=LEFT)
# 右边的图
photo = PhotoImage(file="18.gif")
imgLabel = Label(frame1, image=photo)
imgLabel.pack(side=RIGHT)
# 下边的按钮
theButton = Button(frame2, text="打", command=callback) # 按下显示谁怕谁
theButton.pack() # 自动调整尺寸
frame1.pack(padx=10, pady=10)
frame2.pack(padx=10, pady=10)
mainloop()
from tkinter import *
root = Tk() # 创建root窗口
v = IntVar()
c = Checkbutton(root, text="测试一下", variable=v)
c.pack()
l = Label(root, textvariable=v) # 选中为1
l.pack()
mainloop()
from tkinter import *
root = Tk() # 创建root窗口
GIRLS = ["huzhu", "大猪", "zhu", "猪"]
v = []
for girl in GIRLS:
v.append(IntVar())
b = Checkbutton(root, text=girl, variable=v[-1])
b.pack(anchor=W) # W左对齐按钮
mainloop()
from tkinter import *
root = Tk() # 创建root窗口
v = IntVar()
Radiobutton(root,
text="One",
variable=v,
value=1).pack(anchor=W)
Radiobutton(root,
text="Two",
variable=v,
value=2).pack(anchor=W)
Radiobutton(root,
text="Three",
variable=v,
value=3).pack(anchor=W)
mainloop()
from tkinter import *
root = Tk() # 创建root窗口
LANGS = [
("python", 1),
("Perl", 2),
("Ruby", 3),
("Lua", 4)]
v = IntVar()
v.set(1)
for lang, num in LANGS:
b = Radiobutton(root,
text=lang,
variable=v,
value=num,
indicatoron=False)
b.pack(fill=X)
mainloop()
from tkinter import *
root = Tk() # 创建root窗口
group = LabelFrame(root, text="最好的语言是?",
padx=5, pady=5)
group.pack(padx=10, pady=10)
LANGS = [
("python", 1),
("Perl", 2),
("Ruby", 3),
("Lua", 4)]
v = IntVar()
for lang, num in LANGS:
b = Radiobutton(group,
text=lang,
variable=v,
value=num)
b.pack(anchor=W)
mainloop()
from tkinter import *
root = Tk()
"""输入框"""
e = Entry(root)
e.pack(padx=20, pady=20)
e.delete(0, END) # 清空输入框内容
e.insert(0, "默认文本...") # 插入
mainloop()
from tkinter import *
root = Tk()
Label(root, text="作品:").grid(row=0, column=0) # 第0行第0列
Label(root, text="作者:").grid(row=1, column=0) # 第1行第0列
e1 = Entry(root)
e2 = Entry(root)
e1.grid(row=0, column=1, padx=10, pady=5)
e2.grid(row=1, column=1, padx=10, pady=5)
def show():
print("作品:《%s》" % e1.get())
print("作者:《%s》" % e2.get())
Button(root, text="获取信息", width=10, command=show)\
.grid(row=3, column=0, sticky=W, padx=10, pady=5) # W左边
Button(root, text="退出", width=10, command=root.quit)\
.grid(row=3, column=1, sticky=E, padx=10, pady=5) # E右边
mainloop()
from tkinter import *
root = Tk()
Label(root, text="账号:").grid(row=0, column=0) # 第0行第0列
Label(root, text="密码:").grid(row=1, column=0) # 第1行第0列
v1 = StringVar()
v2 = StringVar()
e1 = Entry(root, textvariable=v1)
e2 = Entry(root, textvariable=v2, show="*")
e1.grid(row=0, column=1, padx=10, pady=5)
e2.grid(row=1, column=1, padx=10, pady=5)
def show():
print("账号:%s" % e1.get())
print("密码:%s" % e2.get())
Button(root, text="芝麻开门", width=10, command=show)\
.grid(row=3, column=0, sticky=W, padx=10, pady=5) # W左边
Button(root, text="退出", width=10, command=root.quit)\
.grid(row=3, column=1, sticky=E, padx=10, pady=5) # E右边
mainloop()
from tkinter import *
master = Tk()
def test():
if e1.get() == "小甲鱼": # 输入的内容为小甲鱼的话
print("正确!")
return True
else:
print("错误!")
e1.delete(0, END) # 清空输入框
return False
v = StringVar()
e1 = Entry(master, textvariable=v,
validate="focusout", validatecommand=test) # 焦点移出的时候
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
mainloop()
from tkinter import *
master = Tk()
v = StringVar()
def test1():
if e1.get() == "小甲鱼": # 输入的内容为小甲鱼的话
print("正确!")
return True
else:
print("错误!")
e1.delete(0, END) # 清空输入框
return False
def test2():
print("我被调用了......")
return True
e1 = Entry(master, textvariable=v, validate="focusout",
validatecommand=test1, invalidcommand=test2)
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
mainloop()
from tkinter import *
master = Tk()
v = StringVar()
def test(content, reason, name):
if e1.get() == "小甲鱼": # 输入的内容为小甲鱼的话
print("正确!")
print(content, reason, name)
return True
else:
print("错误!")
print(content, reason, name)
return False
testCMD = master.register(test)
e1 = Entry(master, textvariable=v, validate="focusout",
validatecommand=(testCMD, '%P', '%v', '%W'))
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
mainloop()
from tkinter import *
master = Tk()
frame = Frame(master)
frame.pack(padx=10, pady=10)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
def test(content):
return content.isdigit() # 返回数字
testCMD = master.register(test)
e1 = Entry(frame, width=10, textvariable=v1, validate="key",
validatecommand=(testCMD, '%P')).grid(row=0, column=0)
Label(frame, text="+").grid(row=0, column=1)
e2 = Entry(frame, width=10, textvariable=v2, validate="key",
validatecommand=(testCMD, '%P')).grid(row=0, column=2)
Label(frame, text="=").grid(row=0, column=3)
e3 = Entry(frame, width=10, textvariable=v3, state="readonly")\
.grid(row=0, column=4)
def calc():
result = int(v1.get()) + int(v2.get())
v3.set(str(result))
Button(frame, text="计算结果", command=calc).grid(row=1, column=2, pady=5)
mainloop()
Label参考博客
参考博客一
参考博客二
frame参考博客
参考博客三