参考链接: Python | 使用Tkinter的简单注册表格
1.创建一个窗口
# 导入模块import tkinter
# 创建该窗口
win = tkinter.Tk()
# 设置窗口标题
win.title("第一个图形程序")
# 设置窗口的大小及位置\\
#参数的形式是"参数1x参数2+参数3+参数4"
# 第一个参数表示界面的宽度
# 第二个参数表示界面的高度
# 第三个参数表示屏幕左边缘该该程序左边缘的距离
# 上边缘距离
# 单位 px
# 1920*1080 表示在屏幕在宽度上上有1920个点, 高度上有1080个点
win.geometry("1700x400+0+200")
# 显示win窗口
win.mainloop()
2.文本控件 Lable
#创建并显示窗口import tkinterwin = tkinter.Tk()win.title("Label标签")win.geometry("300x300+300+300")# 创建label标签# 参数1表示的就是父容器, 就是窗口win# text 指定的是文本内容# height 指定的是标签的高度, 单位约是字符的高度# weidth 指定标签的宽度,# bg 指定背景色# fg 指定字体颜色# font 设置字体,和大小label = tkinter.Label(win,text="胖胖 is a good man",width=6,height = 1,bg="#ffcccc", fg="#ff0000",font=("宋体",30) )# 显示labellabel.pack()
win.mainloop()
3.button按钮
import tkinterwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")# def func():# print("别摸我脚Y子")def func(name): print("别摸%s脚Y子"%(name))# 创建一个button# command用来指定点击按钮后调用的函数# btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=func)# func1 = lambda :func("小明")# 通过lambda传值# btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=lambda :func("小明"))# 退出btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=win.quit)# 显示btn.pack()
win.mainloop()
4.entry输入控件
import tkinterwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")variable = tkinter.Variable()# 创建一个输入控件entry = tkinter.Entry(win,textvariable = variable)# 设置输入框中的字符串variable.set("余钟炜是个啥")# 获取输入框中输入的字符串res = variable.get()print(res)# 显示entry.pack()win.mainloop()
5.点击后获取输入的值
import tkinterwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")def func():# 获取输入框中的值 print(variable.get())variable = tkinter.StringVar()entry = tkinter.Entry(win,textvariable=variable)button = tkinter.Button(win,text="获取值",width=7,bg="#ffcccc",command=func)entry.pack()button.pack()win.mainloop()
6.Combobox下拉框控件
import tkinterfrom tkinter import ttkwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")# 创建一个下拉框控件combox = ttk.Combobox(win)# 给下拉控件设置值(多个)combox["values"] = ("北京","上海","深圳","广州","杭州")# 设置默认选中值# 参数写 索引combox.current(2)# 获取当前的值# res = combox.get()# print(res)def func(event): print(combox.get())# 绑定事件combox.bind("<<ComboboxSelected>>",func)# 显示combox.pack()
win.mainloop()
7.radiobutton单选按钮
import tkinterfrom tkinter import ttkwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")def func(): # print("1") # pass # print(group.get()) value = group.get() if value == 1: print("男") if value == 2: print("女") if value == 3: print("不明")group = tkinter.IntVar()# 创建radiobuttonradio1 = ttk.Radiobutton(win,text="男",variable = group,value = 1,command = func)radio2 = ttk.Radiobutton(win,text="女",variable = group,value = 2,command = func)radio3 = ttk.Radiobutton(win,text="不明",variable= group,value = 3,command = func)# 显示radio1.pack()radio2.pack()radio3.pack()win.mainloop()
8.Checkbutton 多选按钮
import tkinterfrom tkinter import ttkwin = tkinter.Tk()win.title("button按钮控件")win.geometry("300x300+300+300")def getLikes(): # print("1") # 获取选中的爱好 likesList = [] if va1.get() == True: #money被选中 likesList.append("money") if va2.get() == True: likesList.append("power") if va3.get() == True: likesList.append("beautifulgirl") print(likesList)va1 = tkinter.BooleanVar()va2 = tkinter.BooleanVar()va3 = tkinter.BooleanVar()# 创建多个Checkbuttoncb1 = ttk.Checkbutton(win,text="money",variable = va1,command=getLikes)cb2 = ttk.Checkbutton(win,text="power",variable = va2,command=getLikes)cb3 = ttk.Checkbutton(win,text="beautifulgirl",variable = va3,command=getLikes)cb1.pack()cb2.pack()cb3.pack()win.mainloop()
9.表格数据
import tkinterfrom tkinter import ttkwin = tkinter.Tk()win.title("button按钮控件")win.geometry("600x200+300+300")# 创建表格控件table = ttk.Treeview(win)# 设置表头table["columns"] = ("name","age","address")# 设置每一列的宽度table.column("name",width = 100)table.column("age",width = 100)table.column("address",width = 100)# 给表头设置值table.heading("name",text="姓名/name")table.heading("age",text="年龄/age")table.heading("address",text="地址/address")# 插入数据table.insert("",1,text="line1",value=("奥巴马",65,"华盛顿"))table.insert("",2,text="line2",value=("普京",60,"莫斯科"))table.insert("",2,text="line3",value=("莫迪",68,"新德里"))table.pack()
win.mainloop()
10.相对布局
#创建并显示窗口import tkinterwin = tkinter.Tk()win.title("Label标签")win.geometry("600x600+300+300")#label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")# 默认是在中间位置
# label1.pack()# label2.pack(side=tkinter.TOP)
# label3.pack(side=tkinter.BOTTOM)
# label4.pack(side=tkinter.LEFT)
# label4.pack(side=tkinter.RIGHT)
# label2.pack(side=tkinter.TOP,fill="x")
label2.pack(side=tkinter.LEFT,fill="y")
win.mainloop()
11.绝对布局
#创建并显示窗口import tkinterwin = tkinter.Tk()win.title("Label标签")win.geometry("600x600+300+300")#label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")#绝对布局# x指定 宽度上的偏移量# y指定 高度上的偏移量label1.place(x=100,y=0)label2.place(x=500,y=0)label3.place(x=0,y=100)label4.place(x=100,y=100)
win.mainloop()
12.表格布局
#创建并显示窗口import tkinterwin = tkinter.Tk()win.title("Label标签")win.geometry("600x600+300+300")#label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")# 表格布局, column 指定放在哪一列, row表示放在哪一行label1.grid(column=1,row=1)label2.grid(column=2,row=1)label3.grid(row=2,column=2)
win.mainloop()
13.鼠标事件
#创建并显示窗口import tkinterwin = tkinter.Tk()win.title("Label标签")win.geometry("300x300+300+300")#<Button-1>鼠标左键#<Button-3>鼠标右键#<Button-2>鼠标中键#<Double-Button-1>鼠标左键双击#<Double-Button-3>鼠标右键双击#<Double-Button-2>鼠标中键双击#<Triple-Button-1>鼠标左键三击#<Triple-Button-3>鼠标右键三击#<Triple-Button-2>鼠标中键三击btn = tkinter.Button(win,text="点我Y",width=7,bg="#ffcccc")btn.pack()# event 就是鼠标事件, 包含鼠标的操作信息def func(event): print("触发了鼠标右键") print("x:%d,y:%d"%(event.x,event.y))# 给btn 绑定一个鼠标右键# btn.bind("<Button-3>",func)btn.bind("<Double-Button-1>",func)win.mainloop()
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。