接着上篇tkinter的学习记录
1、tkinter之单选和多选
代码语言:javascript复制#coding:utf-8
from tkinter import *
# 定义一个root主窗口
root = Tk()
root.title("应用程序窗口")
# 定义两个变量,用于判断复选框状态
box_1 = False
box_2 = False
# 列表,用于存放选中内容
List_Content = []
def click_c1():
global box_1
# 复选框被选中,box_1置为True
box_1 = not box_1
if box_1:
List_Content.append('Windows')
l['text'] = List_Content
else:
List_Content.remove('Windows')
l['text'] = List_Content
def click_c2():
global box_2
box_2 = not box_2
if box_2:
List_Content.append('Linux')
l['text'] = List_Content
else:
List_Content.remove('Linux')
l['text'] = List_Content
# 定义两个复选框,并绑定相关事件
ck1 = Checkbutton(root, text='Windows', command=click_c1)
ck1.grid(row=0, column=1)
ck2 = Checkbutton(root, text='Linux', command=click_c2)
ck2.grid(row=1, column=1)
l_com = Label(root, text='已选中:')
l_com.grid(row=2, column=0)
l = Label(root, text='')
l.grid(row=2, column=1)
root.mainloop()
2、tkinter之LabelFrame布局
代码语言:javascript复制#coding:utf-8
import tkinter as tk
#标签框架
root = tk.Tk()
root.title("应用程序窗口")
#新建标签框架
group = tk.LabelFrame(root,text="最好的语言是?")
group.pack(padx=10,pady=10)
LANGS = [("python",1),("C",2),("Java",3),("Lua",4)]
v = tk.IntVar()
for lang,num in LANGS:
#选中后 把num的值赋给 variable
b = tk.Radiobutton(group,text=lang,variable = v ,value =num)
b.pack(anchor = tk.W)
l = tk.Label(root,textvariable=v)#显示标签
l.pack(side=tk.LEFT)
tk.mainloop()
3、tkinter实现登录窗口
代码语言:javascript复制#coding:utf-8
from tkinter import *
import tkinter as tk
root = Tk()
root.title('登录窗口')
# 按扭调用的函数,
def reg():
User = e1.get()
Pwd = e2.get()
len_user = len(User)
len_pwd = len(Pwd)
if User == '111' and Pwd == '222':
l_msg['text'] = '登陆成功'
else:
l_msg['text'] = '用户名或密码错误'
e1.delete(0, len_user)
e2.delete(0, len_pwd)
tk.Label(root, text="账号:").grid(row=0, column=0) # 位于第0行,第0列
tk.Label(root, text="密码:").grid(row=1, column=0) # 位于第1行,第0列
v1 = tk.StringVar()
v2 = tk.StringVar()
e1 = tk.Entry(root, textvariable=v1)
e2 = tk.Entry(root, textvariable=v2, show="*") # show:以*显示
e1.grid(row=0, column=1, padx=10, pady=5) # 位于第0行,第1列
e2.grid(row=1, column=1, padx=10, pady=5) # 位于第1行,第1列
# 第三行登陆按扭,command绑定事件
b_login = Button(root, text='登陆', command=reg)
b_login.grid(row=2, column=1, sticky=E)
tk.Button(root,text="退出", command=root.quit)
.grid(row=2,column=2,sticky = tk.E)
def show():
print("账号:%s" �.get())
print("密码:%s" �.get())
tk.Button(root,text="获取信息",width=10,command=show)
.grid(row=2,column=0,sticky = tk.W)
# 登陆是否成功提示
l_msg = Label(root, text='')
l_msg.grid(row=3)
root.mainloop()
4、tkinter实现计算器功能
代码语言:javascript复制#coding:utf-8
import tkinter as tk
root = tk.Tk()
root.title('简易计算器')
v1 = tk.StringVar()
v2 = tk.StringVar()
v3 = tk.StringVar()
def test(content):
return content.isdigit() # 数字会返回true 非数字返回false
testCMD = root.register(test)
# validate触发方法是关键字触发
e1 = tk.Entry(root, width=10, textvariable=v1, validate='key',
validatecommand=(testCMD, "%P")).grid(row=0, column=0)
tk.Label(root, text=" ").grid(row=0, column=1) # 存放 号的
e2 = tk.Entry(root, width=10, textvariable=v2, validate='key',
validatecommand=(testCMD, "%P")).grid(row=0, column=2)
tk.Label(root, text="=").grid(row=0, column=3) # 存放=号的
e3 = tk.Entry(root, textvariable=v3, state="readonly").grid(row=0, column=4) # 只能读取
def calc():
result = int(v1.get()) int(v2.get())
v3.set(str(result))
tk.Button(root, text="计算结果", command=calc).grid(row=1, column=2)
tk.mainloop()