【Python】【Tkinter】

"""

# tkinter 例子1
from tkinter import *

root = Tk()

def callback():
var.set('吹吧你, 我才不信呢')

frame1 = Frame(root)
frame2 = Frame(root)

var = StringVar()
var.set('您做下载的影片含有未成年人限制内容, \n请满18周岁后再点击观看')

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='我已满18周岁', command=callback)
theButton.pack()

frame1.pack(padx=20, pady=50)
frame2.pack(padx=20, pady=50)

mainloop()

# tkinter 例子2 Checkbutton
from tkinter import *

root = Tk()

GIRLS = ['西施', '貂蝉', '王昭君', '杨玉环']

v = []

for girl in GIRLS:
v.append(IntVar())
b = Checkbutton(root, text=girl, variable=v[-1])
b.pack(anchor=W)

mainloop()

# tkinter 例子3 单选框 错误示范
from tkinter import *

root = Tk()

v = IntVar()

Radiobutton(root, text='One', variable=v, value=1).pack(anchor=W)
Radiobutton(root, text='Two', variable=v, value=1).pack(anchor=W)
Radiobutton(root, text='Three', variable=v, value=3).pack(anchor=W)

mainloop()
# 运行结果 第一个或第二个点击的时候 1和2 都被选中了


# tkinter 例子4 单选框 正确示范
from tkinter import *

root = Tk()

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()

# tkinter 例子5 单选框
from tkinter import *

root = Tk()

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)
b.pack(anchor=W)

mainloop()

# tkinter 例子6 单选框
from tkinter import *

root = Tk()

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()
# 运行结果 indicatoron=False会设置按钮的显示方式 fill=X设置横向居中的文字显示方式

# tkinter 例子7 单选框
from tkinter import *

root = Tk()

group = LabelFrame(root, text='最好的脚本语言是?', padx=10, pady=10)
group.pack(padx=20, pady=30)

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()

# tkinter 例子8 文本输入框 账户名密码(星号的输入框)
from tkinter import *

root = Tk()

Label(root, text='账号:').grid(row=0, column=0)
Label(root, text='密码:').grid(row=1, column=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, padx=10, pady=5)
Button(root, text='退出', width=10, command=root.quit)\
.grid(row=3, column=1, padx=10, pady=5)

mainloop()

# tkinter 例子9 输入框格式校验
from tkinter import *

master = Tk()

def test():
if e1.get() == 'snow':
print ('正确')
return True
else:
print ('错误')
e1.delete(0, END)
return False

def test2():
print ('我被调用了')
return True

v = StringVar()

e1 = Entry(master, textvariable=v, validate='focusout',
validatecommand=test,
invalidcommand=test2)
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)

mainloop()


# tkinter 例子10 输入框格式校验

from tkinter import *

master = Tk()

def test(content, reason, name):
if e1.get() == 'snow':
print ('正确')
print (content, reason, name)
return True
else:
print ('错误')
print (content, reason, name)
return False

def test2():
print ('我被调用了')
return True

v = StringVar()

testCMD = master.register(test)
e1 = Entry(master, textvariable=v, validate='focusout',
validatecommand=(testCMD, '%P', '%v', '%W'),
invalidcommand=test2)
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)

mainloop()

# tkinter 例子11 计算器

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=1)

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(result)

Button(frame, text='计算结果', command=calc).grid(row=1, column=2)

mainloop()


# tkinter 例子12 listbox
from tkinter import *

master = Tk()

theLB = Listbox(master, selectmode=EXTENDED)
theLB.pack()

for item in ['鸡蛋', '鸭蛋', '鹅蛋', '李狗蛋']:
theLB.insert(END, item)

theButton = Button(master, text='删除它',
command=lambda x=theLB:x.delete(ACTIVE))
theButton.pack()

mainloop()


# tkinter 例子13 listbox with scrollbar
from tkinter import *

root = Tk()

sb = Scrollbar(root)
sb.pack(side=RIGHT, fill=Y)

lb = Listbox(root, yscrollcommand=sb.set)

for i in range(1000):
lb.insert(END, i)

lb.pack(side=LEFT, fill=BOTH)

sb.config(command=lb.yview)

mainloop()

# tkinter 例子14 scale
# tickinterval 刻度 resolution精度
from tkinter import *

root = Tk()

s1 = Scale(root, from_=0, to=42, tickinterval=5,
resolution=5, length=200)
s1.pack()

s2 = Scale(root, from_=0, to=200,
tickinterval=10, orient=HORIZONTAL,
length=600)
s2.pack()

def show():
print (s1.get(), s2.get())

Button(root, text='获取位置', command=show).pack()

mainloop()

# tkinter 例子15
from tkinter import *

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.insert(INSERT, 'I love \n')
text.insert(END, 'snow')

def show():
print ('哟, 我被点了一下')

b1 = Button(text, text='点我点我', command=show)
text.window_create(INSERT, window=b1)

mainloop()

# tkinter 例子16
from tkinter import *

root = Tk()

text = Text(root, width=30, height=30)
text.pack()

photo = PhotoImage(file='18.gif')

def show():
text.image_create(END, image=photo)

b1 = Button(text, text='点我点我', command=show)
text.window_create(INSERT, window=b1)

mainloop()
"
# tkinter 例子17
from tkinter import *

root = Tk()

text = Text(root, width=30, height=30)
text.insert(INSERT, 'I love snow')
text.mark_set('here', '1.2')
text.insert('here', 'who')
text.insert('here', 'am')
text.pack()

mainloop()

# tkinter 例子18
from tkinter import *

root = Tk()

text = Text(root, width=30, height=30)
text.insert(INSERT, 'I love snow')

text.mark_set('here', '1.2')
text.mark_gravity('here', LEFT)
text.insert('here', 'who ')
text.insert('here', 'am ')
text.pack()

mainloop()

# tkinter 例子19
from tkinter import *

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.insert(INSERT, 'I love snow')

text.tag_add('tag1', '1.7', '1.12', '1.14')
text.tag_config('tag1', background='yellow', foreground='red')

mainloop()

# tkinter 例子20
from tkinter import *

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.tag_config('tag1', background='yellow', foreground='red')
text.tag_config('tag2', foreground='blue')

text.tag_lower('tag1')

text.insert(INSERT, 'i love snow ', ('tag1', 'tag2'))

mainloop()

# tkinter 例子21
from tkinter import *
import webbrowser

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.insert(INSERT, 'I love snow.com!')

text.tag_add('link', '1.7', '1.15')
text.tag_config('link', foreground='blue', underline=True)

def show_arrow_cursor(event):
text.config(cursor='arrow')

def show_xterm_cursor(event):
text.config(cursor='xterm')

def click(event):
webbrowser.open('http://www.fishc.com')

text.tag_bind('link', '<Enter>', show_arrow_cursor)
text.tag_bind('link', '<Leave>', show_xterm_cursor)
text.tag_bind('link', '<Button-1>', click)

mainloop()

# tkinter 例子22 检查内容是否发生变动
from tkinter import *
import hashlib

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.insert(INSERT, 'I love snowe.com!')
contents = text.get('1.0', END)

def getSig(contents):
m = hashlib.md5(contents.encode())
return m.digest()

sig = getSig(contents)

def check():
contents = text.get('1.0', END)
if sig != getSig(contents):
print ('警报:内容发生变动!')
else:
print ('风平浪静')

Button(root, text='检查', command=check).pack()

mainloop()

# tkinter 例子23 查找
from tkinter import *
import hashlib

root = Tk()

text = Text(root, width=30, height=5)
text.pack()

text.insert(INSERT, 'I love snowe.com!')

start = '1.0'

def getIndex(text, index):
return tuple(map(int, str.split(text.index(index), '.')))

while True:
pos = text.search('o', start, stopindex=END)
if not pos:
break
print ('找到啦, 位置是:', getIndex(text, pos))
start = pos + '+1c'

mainloop()

# tkinter 例子24 撤销
from tkinter import *
import hashlib

root = Tk()
#undo=True 设置可撤销 autoseparators=False设置撤销分隔符不是默认的换行,而是自定义
text = Text(root, width=30, height=5, undo=True, autoseparators=False)
text.pack()

text.insert(INSERT, 'I love snowe.com!')

def callback(event):
text.edit_separator()

text.bind('<Key>', callback)

def show():
text.edit_undo()

Button(root, text='撤销', command=show).pack()

mainloop()

# tkinter 例子25 画布
from tkinter import *

root = Tk()

w = Canvas(root, width=200, height=100, background='gray')
w.pack()

w.create_line(0, 50, 200, 50, fill='yellow')
w.create_line(100, 0, 100, 100, fill='red', dash=(4, 4))
w.create_rectangle(50, 25, 150, 75, fill='blue')

mainloop()



# tkinter 例子26 画布 操作
from tkinter import *

root = Tk()

w = Canvas(root, width=200, height=100, background='gray')
w.pack()

line1 = w.create_line(0, 50, 200, 50, fill='yellow')
line2 = w.create_line(100, 0, 100, 100, fill='red', dash=(4, 4))
rect1 = w.create_rectangle(50, 25, 150, 75, fill='blue')

w.coords(line1, 0, 25, 200, 25)
w.itemconfig(rect1, fill='red')
w.delete(line2)

Button(root, text='删除全部', command=(lambda x=ALL:w.delete(x))).pack()

mainloop()

# tkinter 例子27 画布 文本 椭圆
from tkinter import *

root = Tk()

w = Canvas(root, width=200, height=100, background='gray')
w.pack()

w.create_rectangle(40, 20, 160, 80, dash=(4, 4))
w.create_oval(40, 20, 160, 80, fill='pink') #椭圆
w.create_text(100, 50, text='snow')

mainloop()

# tkinter 例子28 画布 圆
from tkinter import *

root = Tk()

w = Canvas(root, width=200, height=100, background='gray')
w.pack()

w.create_rectangle(40, 20, 160, 80, dash=(4, 4))
w.create_oval(70, 20, 130, 80, fill='pink') #椭圆
w.create_text(100, 50, text='snow')

mainloop()
"""
# tkinter 例子29 五角星
'''
根据中心点坐标导出五角星五个点的坐标
中心点(x, y)
A 左上角 B 上角 C 右上角 D 右下角 E 左下角

 

 


'''
"""


# tkinter 例子29 五角星
from tkinter import *
import math as m

root = Tk()

w = Canvas(root, width=200, height=100)
w.pack()

center_x = 100
center_y = 50
r = 50

points = [
# 左上角
center_x - int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
# 右上角
center_x + int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
# 左下角
center_x - int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi / 5)),
# 顶角
center_x,
center_y - r,
# 右下角
center_x + int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi / 5))
]

w.create_polygon(points, outline='green', fill='')

mainloop()

# tkinter 例子30 画线
from tkinter import *

root = Tk()

w = Canvas(root, width=400, height=200)
w.pack()

def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
w.create_oval(x1, y1, x2, y2, fill='red')

w.bind('<B1-Motion>', paint)

Label(root, text='按住鼠标左键并移动,开始绘制你的蓝图吧~~')

mainloop()

# tkinter 例子31 menu 执行时候闪退了 原因不明
from tkinter import *

root = Tk()

def callback():
print ('你好')

menubar = Menu(root)

filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label='打开', command=callback)
filemenu.add_command(label='保存', command=callback)
filemenu.add_separator()
filemenu.add_command(label='退出', command=root.quit)
menubar.add_cascade(label='文件', menu=filemenu)

editmenu = Menu(menubar, tearoff=False)
editmenu.add_command(label='剪切', command=callback)
editmenu.add_command(label='拷贝', command=callback)
editmenu.add_command(label='黏贴', command=callback)
menubar.add_cascade(label='编辑', menu=menubar)

root.config(menu=menubar)

mainloop()


# tkinter 例子32 鼠标右键 出现menu
from tkinter import *

root = Tk()

def callback():
print ('你好')

menubar = Menu(root)
menubar.add_command(label='撤销', command=callback)
menubar.add_command(label='重做', command=root.quit)

frame = Frame(root, width=512, height=512)
frame.pack()

def popup(event):
menubar.post(event.x_root, event.y_root)

frame.bind('<Button-1>', popup)

mainloop()

# tkinter 例子33 checkbutton&radiobutton
from tkinter import *

root = Tk()

def callback():
print ('你好')

menubar = Menu(root)

openVar = IntVar()
saveVar = IntVar()
quitVar = IntVar()

filemenu = Menu(menubar, tearoff=True)
filemenu.add_checkbutton(label='打开', command=callback, variable=openVar)
filemenu.add_checkbutton(label='保存', command=callback, variable=saveVar)
filemenu.add_separator()
filemenu.add_checkbutton(label='退出', command=root.quit, variable=quitVar)
menubar.add_cascade(label='文件', menu=filemenu)

editVar = IntVar()

editmenu = Menu(menubar, tearoff=False)
editmenu.add_radiobutton(label='剪切', command=callback,
variable=editVar, value=1)
editmenu.add_radiobutton(label='拷贝', command=callback,
variable=editVar, value=2)
editmenu.add_radiobutton(label='黏贴', command=callback,
variable=editVar, value=3)
menubar.add_cascade(label='编辑', menu=editmenu)

root.config(menu=menubar)

mainloop()

# tkinter 例子34 Menubutton
from tkinter import *

root = Tk()

def callback():
print ('你好')

mb = Menubutton(root, text='点我', relief=RAISED)
mb.pack()

filemenu = Menu(mb, tearoff=False)
filemenu.add_command(label='打开', command=callback)
filemenu.add_command(label='保存', command=callback)
filemenu.add_separator()
filemenu.add_command(label='退出', command=root.quit)

mb.config(menu=filemenu)

mainloop()

# tkinter 例子35 OptionMenu
from tkinter import *

root = Tk()

variable = StringVar()
variable.set('one')

w = OptionMenu(root, variable, 'one', 'two', 'three')
w.pack()

mainloop()

# tkinter 例子36 OptionMenu 列表可变
from tkinter import *

OPTIONS = [
'California',
'458',
'FF',
'snow'
]

root = Tk()

variable = StringVar()
variable.set(OPTIONS[0])

w = OptionMenu(root, variable, *OPTIONS)
w.pack()

mainloop()

# tkinter 例子37 事件 鼠标事件
from tkinter import *

root = Tk()

def callback(event):
print ('点击位置。', event.x, event.y)

frame = Frame(root, width=200, height=200)
frame.bind('<Button-1>', callback)
frame.pack()

mainloop()

# tkinter 例子38 事件 按键事件
from tkinter import *

root = Tk()

def callback(event):
print (event.char)

frame = Frame(root, width=200, height=200)
frame.bind('<Key>', callback)
frame.focus_set()
frame.pack()

mainloop()

# tkinter 例子39 事件 鼠标事件
from tkinter import *

root = Tk()

def callback(event):
print ('当前位置:', event.x, event.y)

frame = Frame(root, width=200, height=200)
frame.bind('<Motion>', callback)
frame.pack()

mainloop()

# tkinter 例子40 Message
from tkinter import *

root = Tk()

w1 = Message(root, text='这事一则消息', width=100)
w1.pack()

w2 = Message(root, text='这是一\n则骇人听闻的长长长长长长长长消息',
width=100)
w2.pack()

mainloop()

# tkinter 例子41 Spinbox
from tkinter import *

root = Tk()

w = Spinbox(root, values=('snow', 'SNOW', 'hi'))
w.pack()

mainloop()

# tkinter 例子42 PaneWidnow 一个pane 垂直布局
from tkinter import *

root = Tk()

m = PanedWindow(orient=VERTICAL)
m.pack(fill=BOTH, expand=1)

top = Label(m, text='top pane')
m.add(top)

bottom = Label(m, text='bottom pane')
m.add(bottom)

mainloop()

# tkinter 例子43 PaneWidnow 一个大的套一个小的 某个微妙的位置可拖动 但很难找
下一个例子会让他显示出来

from tkinter import *

root = Tk()

m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)

left = Label(m1, text='left pane')
m1.add(left)

m2 = PanedWindow(orient=VERTICAL)
m1.add(m2)

top = Label(m2, text='top pane')
m2.add(top)

bottom = Label(m2, text='bottom pane')
m2.add(bottom)

mainloop()

# tkinter 例子44 PaneWidnow 显示手柄
from tkinter import *

root = Tk()

m1 = PanedWindow(showhandle=True, sashrelief=SUNKEN)
m1.pack(fill=BOTH, expand=1)

left = Label(m1, text='left pane')
m1.add(left)

m2 = PanedWindow(orient=VERTICAL, showhandle=True, sashrelief=SUNKEN)
m1.add(m2)

top = Label(m2, text='top pane')
m2.add(top)

bottom = Label(m2, text='bottom pane')
m2.add(bottom)

mainloop()

# tkinter 例子45 TopLevel

from tkinter import *

root = Tk()

def create():
top = Toplevel()
top.title('FishC Demo')

msg = Message(top, text='i love snow')
msg.pack()

Button(root, text='创建顶级窗口', command=create).pack()

mainloop()

# tkinter 例子46
from tkinter import *

root = Tk()

def create():
top = Toplevel()
top.title('FishC Demo')

msg = Message(top, text='i love snow')
msg.pack()

Button(root, text='创建顶级窗口', command=create).pack()

mainloop()

"""













































posted @ 2021-03-03 17:56  素人渔芙2017  阅读(137)  评论(0编辑  收藏  举报