可以编写出带页面的程序,不过由于界面不够美化一般都是安装程序中用到这些,仅仅对每个控件进行代码演示。
import tkinter
# 创建主窗口
win = tkinter.Tk()
# 设置窗口标题
win.title("sunck")
# 设置窗口大小与位置
win.geometry("400x400+200+0")
# 进入消息循环
# 程序运行起来
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
# win表示主窗体
# text:文本内容
# bg:背景颜色
# fg:字体颜色
# font:字体型号与大小
# anchor:位置 N NE E SE S SW W NW CENTER
# width:空间宽度
# wraplength:指定text中文本多少宽度后开始换行
# justify:text中多行文本的对齐方式
l = tkinter.Label(win, text="sunckisagoodman", bg="pink", fg="red", font=("Arial",12), anchor=tkinter.SW, width=10, height=3, wraplength = 30, justify="right")
# 显示控件
l.pack()
win.mainloop()
import tkinter
def func():
print("sunck is a good man")
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
# 创建按钮
# 显示窗口
# 文本
# 点击按钮响应的方法
button=tkinter.Button(win, text="按钮", width=10, height=10, command=func)
button.pack()
button1=tkinter.Button(win, text="按钮1", command=lambda :print("sunck is a nice man"))
button1.pack()
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
# 绑定变量
var = tkinter.Variable()
e = tkinter.Entry(win, textvariable=var, show="*")
e.pack()
# 设置文本框中的值
var.set("sunck is a good man")
# 获取文本框中的值
print(var.get())
print(e.get())
win.mainloop()
import tkinter
def func():
print(ve.get())
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
ve = tkinter.Variable()
e = tkinter.Entry(win, textvariable=ve)
e.pack()
button=tkinter.Button(win, text="显示", command=func)
button.pack()
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
#height:控制显示行数
t = tkinter.Text(win, width=30, height=4)
t.pack()
# t.pack(side=tkinter.LEFT, fill=tkinter.Y) #滚动条写法
info = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished."""
#插入
t.insert(tkinter.INSERT, info)
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("sunck")
# win.geometry("400x400+200+0")
s = tkinter.Scrollbar()
s.pack(side=tkinter.RIGHT, fill=tkinter.Y)
t = tkinter.Text(win, width=30, height=4)
t.pack(side=tkinter.LEFT, fill=tkinter.Y)
s.config(command=t.yview)
t.config(yscrollcommand=s.set)
info = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished."""
t.insert(tkinter.INSERT, info)
win.mainloop()
from tkinter import *
def updata():
message = ""
if hobby1.get() == 1:
message += "Your Hobby is 1\n"
if hobby2.get() == 1:
message += "Your Hobby is 2\n"
if hobby3.get() == 1:
message += "Your Hobby is 3\n"
txt.delete(0.0, END)#全部删除txt内容
txt.insert(0.0, message)
win = Tk()
win.title("sunck")
label=Label(win,text = "Please check")
label.pack()
hobby1 = BooleanVar()
checkb1 = Checkbutton(win,text = "hobby1",variable = hobby1,command = updata)
checkb1.pack()
hobby2 = BooleanVar()
checkb2 = Checkbutton(win,text = "hobby2",variable = hobby2,command = updata)
checkb2.pack()
hobby3 = BooleanVar()
checkb3 = Checkbutton(win,text = "hobby3",variable = hobby3,command = updata)
checkb3.pack()
txt = Text(win,width = 50,height = 5,wrap = WORD)
txt.pack()
win.mainloop()
from tkinter import *
def updata():
print(v.get())
win = Tk()
win.title("sunck")
win.geometry("400x400+200+0")
v = IntVar()
Radiobutton(win, text="One", variable=v, value=1, command=updata).pack()
Radiobutton(win, text="Two", variable=v, value=2, command=updata).pack()
mainloop()
from tkinter import *
win = Tk()
win.title("sunck")
# win.geometry("400x400+200+0")
# 1、创建一个Listbox,向其中添加三个item
# lb = Listbox(win, selectmode = BROWSE)
# for item in ['good', 'nice', 'handsom', "grace", "noble", "sense"]:
# #按顺序向后添加
# lb.insert(END, item)
# lb.pack()
# 在开始添加
# lb.insert(ACTIVE, "cool")
# 只添加一项将[]作为一个item
# lb.insert(END,["1","2","3"])
# 删除 第一个为开始的索引值;第二个为结束的索引值,如果不指定则只删除第一个索引项
# lb.delete(1,3)
# lb.delete(1)
# 选中函数,selection_set函数有两个参数第一个为开始的索引;第二个为结束的索引,如果不指定则只选中第一个参数指定的索引项
# lb.select_set(2, 4)
# 取消选中
# lb.select_clear(2,4)
# 得到当前Listbox中的item个数
# print(lb.size())
# 返回指定索引的项,参数第一个为开始的索引;第二个为结束的索引,如果不指定则只选中第一个参数指定的索引项
# print(lb.get(1, 3))
# print(lb.get(1))
# 返回当前返回的项的索引,不是item的值
# print(lb.curselection())
# 判断 一个项是否被选中,使用索引
# print(lb.selection_includes(2))
# print(lb.selection_includes(5))
# 2、与BROWSE相似 的为SINGLE,但不支持鼠标移动选中位置
# v = StringVar()
# lb = Listbox(win, selectmode = SINGLE, listvariable=v)
# for item in ['good', 'nice', 'handsom']:
# lb.insert(END, item)
# lb.pack()
# 打印当前列表中的项值
# print(v.get())
# 改变v的值,使用tuple可以与item对应
# v.set(('1000','200'))
# 事件绑定
# def printList(event):
# print(lb.get(lb.curselection()))
# lb.bind('
# 3、使用selectmode = EXPANDED使用Listbox来支持Shift和Control
# lb = Listbox(win, selectmode = EXTENDED)
# for item in ['good', 'nice', 'handsom', "grace", "noble", "sense", 1, 2, 3, 4, 5, 6]:
# lb.insert(END, item)
# lb.pack()
# # 运行程序,点中“python",shift + 点击"widget",会选中所有的item
# # 运行程序,点中"python",control + 点击"widget",会选中python和widget,第二项tkinter处于非选中状态
# # 滚动条控件
# # 当内容超过可视化区域时使用,如列表框
# scrl = Scrollbar(win)
# scrl.pack(side=RIGHT,fill=Y)
# # 指定Listbox的yscrollbar的回调函数为Scrollbar的set,表示滚动条在窗口变化时实时更新
# lb.configure(yscrollcommand=scrl.set)
# lb.pack(side=LEFT,fill=BOTH)
# # # 指定Scrollbar的command的回调函数是Listbar的yview
# scrl['command'] = lb.yview
# 4、创建一个可以多选的Listbox,使用属性selectmaod
lb = Listbox(win, selectmode = MULTIPLE, )
for item in ['good', 'nice', 'handsom']:
lb.insert(END, item)
lb.pack()
win.mainloop()
# Tkinter 中的 Scale 控件是一种可供用户通过拖动指示器改变变量值的控件. 这种控件可以水平放置, 也可以竖直放置
from tkinter import *
win = Tk()
win.title("sunck")
win.geometry("400x400+200+0")
# 竖
# from起始值
# to结束值
# tickinterval选择值将会变为该数值的倍数
# length 竖直时为高度,水平是为长度
scale1 = Scale(win, from_=0, to=40, tickinterval=8, length=300)
scale1.pack()
#默认值为起始值,可以通过set赋值
scale1.set(20)
# 获取值
# def show():
# print(scale1.get())
# Button(win, text="show", command=show).pack()
#横 orient=HORIZONTAL
# scale2 = Scale(win, from_=0, to=200, orient=HORIZONTAL)
# scale2.pack()
win.mainloop()
from tkinter import *
win = Tk()
win.title("sunck")
win.geometry("400x400+200+0")
def change():
print(v.get())
v = StringVar()
# from_:最小值
# to:最大值
# increment:步长
# values:每次更新值将使用values指定的值,不要和from_与to同时使用values=(0,2,4,6,8)
sp = Spinbox(win, from_=0, to=100, increment=1, textvariable=v, command=change)
sp.pack()
#设置值
v.set(20)
#取值
print(v.get())
win.mainloop()
from tkinter import *
def callback():
print("sunck is a good man")
win = Tk()
win.title("sunck")
win.geometry("400x400+200+0")
menubar = Menu(win)
win.config(menu=menubar)
filemenu = Menu(menubar, tearoff=False)
for item in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript', "退出"]:
if item == "退出":
filemenu.add_separator()
filemenu.add_command(label=item, command=win.quit)
else:
filemenu.add_command(label=item, command=callback)
filemenu2 = Menu(menubar, tearoff=False)
#accelerator快捷键
filemenu2.add_command(label="颜色设置", command=callback, accelerator='Ctrl+N')
filemenu2.add_command(label="字体设置")
menubar.add_cascade(label="语言", menu=filemenu)
menubar.add_cascade(label="设置", menu=filemenu2)
win.mainloop()
from tkinter import *
win = Tk()
win.title("sunck")
win.geometry("400x400+200+0")
menubar = Menu(win)
def callback():
print('sunck is a good man')
filemenu = Menu(menubar, tearoff=0)
for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:
filemenu.add_command(label=k, command=callback)
filemenu.add_separator()
menubar.add_cascade(label='语言', menu=filemenu)
def popup(event):
menubar.post(event.x_root, event.y_root)
win.bind('
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
tkinter.Label(win, text="frame", bg="red", font=("Arial",15)).pack()
frm = tkinter.Frame(win)
frm.pack()
# #left
frm_L = tkinter.Frame(frm)
tkinter.Label(frm_L, text="左上", bg="pink", font=("Arial",12)).pack(side=tkinter.TOP)
tkinter.Label(frm_L, text="左下", bg="green", font=("Arial",12)).pack(side=tkinter.TOP)
frm_L.pack(side=tkinter.LEFT)
#right
frm_R = tkinter.Frame(frm)
tkinter.Label(frm_R, text="右上", bg="yellow", font=("Arial",12)).pack(side=tkinter.TOP)
tkinter.Label(frm_R, text="右下", bg="purple", font=("Arial",12)).pack(side=tkinter.TOP)
frm_R.pack(side=tkinter.RIGHT)
win.mainloop()
import tkinter
from tkinter import ttk
def func(*args):
# 当前选中的值
print(comboxlist.get())
win = tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
var = tkinter.StringVar()
comboxlist = ttk.Combobox(win, textvariable = var)
# 设置下拉数据
comboxlist["value"] = ("1","2","3","4","5")
# 默认现实的值的下标
comboxlist.current(0)
# 绑定事件
comboxlist.bind("<
comboxlist.pack()
win.mainloop()
import tkinter
from tkinter import ttk
win=tkinter.Tk()
win.title("sunck")
win.geometry("800x400+200+0")
#表格
tree=ttk.Treeview(win)
tree.pack()
#表示列,不显示
tree["columns"]=("name","age","height")
tree.column("name",width=100)
tree.column("age",width=100)
tree.column("height",width=100)
#显示表头
tree.heading("name",text="姓名")
tree.heading("age",text="年龄")
tree.heading("height",text="身高")
# #插入行
tree.insert("",0,text="line1" ,values=("1","2","3"))
tree.insert("",1,text="line2" ,values=("1","2","3"))
tree.insert("",2,text="line3" ,values=("1","2","3"))
tree.insert("",3,text="line4" ,values=("1","2","3"))
win.mainloop()
import tkinter
from tkinter import ttk
win=tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
tree=ttk.Treeview(win)
tree.pack()
#一级树枝
treeF1=tree.insert("",0,"中国",text="中国CHI",values=("F1"))
treeF2=tree.insert("",1,"美国",text="美国USA",values=("F2"))
treeF3=tree.insert("",2,"英国",text="英国ENG",values=("F3"))
#二级树枝
threeF1_1=tree.insert(treeF1,0,"黑龙江",text="中国黑龙江",values=("F1_1"))
threeF1_2=tree.insert(treeF1,1,"吉林",text="中国吉林",values=("F1_2"))
threeF1_3=tree.insert(treeF1,2,"辽宁",text="中国辽宁",values=("F1_3"))
threeF1_4=tree.insert(treeF1,3,"北京",text="中国北京",values=("F1_4"))
threeF2_1=tree.insert(treeF2,0,"黑龙江1",text="美国黑龙江",values=("F2_1"))
threeF2_2=tree.insert(treeF2,1,"吉林1",text="美国吉林",values=("F2_2"))
threeF2_3=tree.insert(treeF2,2,"辽宁1",text="美国辽宁",values=("F2_3"))
threeF2_4=tree.insert(treeF2,3,"北京1",text="美国北京",values=("F2_4"))
threeF3_1=tree.insert(treeF3,0,"黑龙江2",text="英国黑龙江",values=("F3_1"))
threeF3_2=tree.insert(treeF3,1,"吉林2",text="英国吉林",values=("F3_2"))
threeF3_3=tree.insert(treeF3,2,"辽宁2",text="英国辽宁",values=("F3_3"))
threeF3_4=tree.insert(treeF3,3,"北京2",text="英国北京",values=("F3_4"))
#三级树枝
threeF1_1_1=tree.insert(threeF1_1,0,"哈尔滨",text="黑龙江哈尔滨",values=("F1_1_1_1"))
threeF1_1_2=tree.insert(threeF1_1,1,"五常",text="黑龙江五常",values=("F1_1_1_2"))
threeF1_1_3=tree.insert(threeF1_1,2,"山河",text="黑龙江山河",values=("F1_1_1_3"))
win.mainloop()
import tkinter
win=tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
label1=tkinter.Label(win,text="good",bg="blue")
label2=tkinter.Label(win,text="nice",bg="yellow")
label3=tkinter.Label(win,text="handsome",bg="red")
# 绝对布局,窗口变化不影响
label1.place(x=10, y=10)
label2.place(x=50, y=50)
label3.place(x=100, y=100)
# label1.place(x=10,y=10,anchor=tkinter.NW)
# #输入N,距离N10,W靠近,输入W,距离W10 ,N靠近
# #输入NW,N,W都距离10
# label2.place(x=10,y=10,anchor=tkinter.NW)
# label3.place(x=10,y=300,anchor=tkinter.W)
win.mainloop()
import tkinter
win=tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
label1=tkinter.Label(win,text="good",bg="blue")
label2=tkinter.Label(win,text="nice",bg="yellow")
label3=tkinter.Label(win,text="handsome",bg="red")
# fill窗口变化Y方向同步变化 X Y BOTH
label1.pack(fill=tkinter.Y, side=tkinter.LEFT)
# side:贴边 TOP BOTTOM CENTER LEFT RIGHT
label2.pack(fill=tkinter.X, side=tkinter.TOP)
label3.pack()
win.mainloop()
import tkinter
win=tkinter.Tk()
win.title("sunck")
win.geometry("400x400+200+0")
label1=tkinter.Label(win,text="good",bg="blue")
label2=tkinter.Label(win,text="nice",bg="yellow")
label3=tkinter.Label(win,text="handsom",bg="red")
label4=tkinter.Label(win,text="nobo",bg="green")
#表格
label1.grid(row=0,column=0)
label2.grid(row=0,column=1)
label3.grid(row=1,column=0)
label4.grid(row=1,column=1)
win.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print(event.x,event.y)
bt1 = Button(root,text = 'leftmost button')
bt1.bind('
bt2 = Button(root,text = 'middle button')
bt2.bind('
bt3 = Button(root,text = 'rightmost button')
bt3.bind('
bt4 = Button(root,text = 'double click')
bt4.bind('
bt5 = Button(root, text = 'triple click')
bt5.bind('
bt1.pack()
bt2.pack()
bt3.pack()
bt4.pack()
bt5.pack()
root.mainloop()
#测试鼠标的移动事件
#
from tkinter import *
win = Tk()
def printCoords(event):
print(event.x,event.y)
bt1 = Button(win,text = 'leftmost button')
bt1.bind('
bt2 = Button(win,text = 'middle button')
bt2.bind('
bt3 = Button(win,text = 'rightmost button')
bt3.bind('
bt1.grid()
bt2.grid()
bt3.grid()
win.bind('
win.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print(event.x,event.y)
bt1 = Button(root,text = 'leftmost button')
bt1.bind('
bt2 = Button(root,text = 'middle button')
bt2.bind('
bt3 = Button(root,text = 'rightmost button')
bt3.bind('
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print(event.x,event.y)
bt1 = Button(root,text = 'leftmost button')
bt1.bind('
bt1.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print(event.x,event.y)
bt1 = Button(root,text = 'leftmost button')
bt1.bind('
bt1.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print('event.char = ',event.char)
print('event.keycode = ',event.keycode)
bt1 = Button(root,text = 'Press BackSpace')
bt1.bind('
bt2 = Button(root,text = 'Press Enter')
bt2.bind('
bt3 = Button(root,text = 'F5')
bt3.bind('
bt4 = Button(root,text = 'Left Shift')
bt4.bind('
bt5 = Button(root,text = 'Right Shift')
bt5.bind('
bt1.focus_set()
bt1.grid()
bt2.grid()
bt3.grid()
bt4.grid()
bt5.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print('event.char = ',event.char)
print('event.keycode = ',event.keycode)
bt1 = Button(root,text = 'Press BackSpace')
bt1.bind('
bt1.focus_set()
bt1.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print('event.char = ',event.char)
print('event.keycode = ',event.keycode)
bt1 = Button(root,text = 'Press BackSpace')
bt1.bind('a',printCoords)
bt1.focus_set()
bt1.grid()
root.mainloop()
from tkinter import *
root = Tk()
def printCoords(event):
print('event.char = ',event.char)
print('event.keycode = ',event.keycode)
bt1 = Button(root,text = 'Press Shift - Up')
bt1.bind('
bt2 = Button(root,text = 'Control-Alt-a')
bt2.bind('
bt3 = Button(root,text = 'Control-Alt')
bt3.bind('
bt1.focus_set()
bt1.grid()
bt2.grid()
root.mainloop()