参考资料:
https://www.cnblogs.com/wwf828/p/7418181.html
Tkinter是Python进行窗口视窗设计的模块,具有简单的结构,多平台,多系统的兼容性.
窗口主体框架
import tkinter as tk
window = tk.Tk()
window.title("tkinter window")#窗口标题
window.geometry("300x300")#窗口大小
window.mainloop()#让窗口活起来
l = tk.Label(window,
text="this is label",#标签的文字
bg='yellow', #背景颜色
font=('Arial',12),#字体和字体大小
width=15,height=2)#标签长宽
l.pack()#固定窗口位置
注意:定义触发事件时的函数(注意:因为Python的执行顺序是从上往下,所以函数一定要放在按钮的上面
def hit_me():
pass
b = tk.Button(window,
text='hit me', # 显示在按钮上的文字
width=15, height=2,
command=hit_me) # 点击按钮式执行的命令
b.pack() # 按钮位置
使用tk.StringVar() #文字变量储存器
将text属性改成textvariable
var = tk.StringVar()#文字变量储存器
l = tk.Label(window,
textvariable=var,#标签的文字
bg='yellow', #背景颜色
font=('Arial',12),#字体和字体大小
width=15,height=2)#标签长宽
l.pack()#固定窗口位置
完整代码:
import tkinter as tk
window = tk.Tk()
window.title("tkinter window")#窗口标题
window.geometry("300x100")#窗口大小
var = tk.StringVar()#文字变量储存器
l = tk.Label(window,
textvariable=var,#标签的文字
bg='yellow', #背景颜色
font=('Arial',12),#字体和字体大小
width=15,height=2)#标签长宽
l.pack()#固定窗口位置
hit_m = False
def hit_me():
global hit_m
if hit_m == False:
hit_m=True
var.set('hit me')
else:
hit_m = False
var.set('')
b = tk.Button(window,
text='button',
width=15,height=2,
command=hit_me)
b.pack()
window.mainloop()#让窗口活起来
# show属性控制输入框的显示,当show=None,输入什么显示什么,show='*',显示的是*******
e = tk.Entry(window,
show="*")
e.pack()
t = tk.Text(window,height=2)
t.pack()
从上至下分别为定义的Entry、Button、Button和Text。两个Button分别命名为insert point和insert end,获取Entry中输入的内容并且分别在光标位置、末尾插入,内容显示在Text中。
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('300x300')
e = tk.Entry(window,show=None)
e.pack()
def insert_point():
var = e.get()
t.insert('insert',var)
def insert_end():
var = e.get()
t.insert('end',var)
b1 = tk.Button(window,text='insert point',width=15,
height=2,command=insert_point)
b1.pack()
b2 = tk.Button(window,text='insert end',
command=insert_end)
b2.pack()
t = tk.Text(window,height=2)
t.pack()
window.mainloop()
var2 = tk.StringVar()
var2.set((1,2,3,4,5,6,7,8,9,10)) #为变量设置值
lb = tk.Listbox(window,listvariable=var2)#将var2的值赋给Listbox
lb.pack()
import tkinter as tk
window = tk.Tk()
window.title('window')
window.geometry('300x300')
var1 = tk.StringVar() #创建变量
l =tk.Label(window,bg='yellow',width=4,textvariable=var1)
l.pack()
def print_selection():
value = lb.get(lb.curselection()) #获取当前选中的文本
var1.set(value) #为label设置值
b1 = tk.Button(window, text='print selection', width=15,
height=2, command=print_selection)
b1.pack()
var2 = tk.StringVar()
var2.set((11,22,33,44)) #为变量设置值
#创建Listbox
lb = tk.Listbox(window, listvariable=var2) #将var2的值赋给Listbox
#创建一个list并将值循环添加到Listbox控件中
list_items = [1,2,3,4]
for item in list_items:
lb.insert('end', item) #从最后一个位置开始加入值
lb.insert(1, 'first') #在第一个位置加入'first'字符
lb.insert(2, 'second') #在第二个位置加入'second'字符
lb.delete(2) #删除第二个位置的字符
lb.pack()
#显示主窗口
window.mainloop()
s = tk.Scale(window, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()
参数from_=5,to=11的意思就是从5到11,即这个滚动条最小值为5,最大值为11(这里使用from_是因为在python中有from这个关键词)
参数orient=tk.HORIZONTAL在这里就是设置滚动条的方向,这里HORIZONTAL就是横向。
参数length这里是指滚动条部件的长度,而此处的length=200,是指我们常用的像素为单位,即长度为200个像素
参数resolution=0.01这里我们可以借助数学题来理解,我们做的很多数学题都会让我们来保留几位小数,此处的0.01就是保留2位小数,即效果图中的5.00 9.00等等后面的两位小数,如果保留一位就是resolution=0.1 这里的showvalue就是设置在滚动条上方的显示。
参数tickinterval设置的就是坐标的间隔
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('300x300')
l=tk.Label(window,bg='yellow',width=20,text='empty')
l.pack()
def print_selection(v):
l.config(text='you have selected '+v)
s=tk.Scale(window,label='try me',from_=5,to=11,orient=tk.HORIZONTAL,
length=200,showvalue=0,tickinterval=3,resolution=0.01,command=print_selection)
s.pack()
window.mainloop()
var = tk.StringVar()
def print_selection():
pass
r1 = tk.Radiobutton(window, text='Option A',
variable=var, value='A',
command=print_selection)
r1.pack()
var.get()就是获取到变量 var 的值
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
var = tk.StringVar()
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
l.config(text='you have selected ' + var.get())
r1 = tk.Radiobutton(window, text='Option A',
variable=var, value='A',
command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B',
variable=var, value='B',
command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C',
variable=var, value='C',
command=print_selection)
r3.pack()
window.mainloop()
Checkbutton类似于多选,可以选中多个
选中时value为1,即onvalue=1,未选中时offvalue=0
def btcheck():
print("Checkbutton")
var1 = tk.IntVar()
ck = tk.Checkbutton(window,text='python',variable=var1,onvalue=1,offvalue=0,command=btcheck)
ck.pack()
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('300x300')
l=tk.Label(window,bg='yellow',width=20,text='empty')
l.pack()
def print_selection():
if(var1.get()==1)&(var2.get()==0):
l.config(text='I love only Python')
elif(var1.get()==0)&(var2.get()==1):
l.config(text='I love only C++')
elif(var1.get()==0)&(var2.get()==0):
l.config(text='I do not love either')
else:
l.config(text='I love both')
var1=tk.IntVar()
var2=tk.IntVar()
c1=tk.Checkbutton(window,text='Python',variable=var1,onvalue=1,offvalue=0,
command=print_selection)
c2=tk.Checkbutton(window,text='C++',variable=var2,onvalue=1,offvalue=0,
command=print_selection)
c1.pack()
c2.pack()
window.mainloop()
ca = tk.Canvas(window,bg='blue',width=100,height=100)
line=ca.create_line(x0,y0,x1,y1)
oval=ca.create_oval(x0,y0,x1,y1,fill='yellow')
arc=ca.create_arc(x0+30,y0+30,x1+30,y1+30,start=0,extent=180,fill='green')
rect=ca.create_rectangle(100,30,100+20,30+20)
ca.pack()
#点击按钮,方框下移
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('300x300')
canvas=tk.Canvas(window,bg='blue',height=150,width=200)
x0,y0,x1,y1=90,90,120,120
line=canvas.create_line(x0,y0,x1,y1)
oval=canvas.create_oval(x0,y0,x1,y1,fill='yellow')
arc=canvas.create_arc(x0+30,y0+30,x1+30,y1+30,start=0,extent=180,fill='green')
rect=canvas.create_rectangle(100,30,100+20,30+20)
canvas.pack()
def moveit():
canvas.move(rect,0,2)
b=tk.Button(window,text='move',command=moveit).pack()
window.mainloop()
def do_job():
print("menu")
#定义一个Menu
menubar = tk.Menu(window)
# 接着在menubar上创建File选项,首先同样定义一个Menu,此时的Menu是放置在menubar上的,并通过add_cascade(cascade:级联)放置在menubar上
filemenubar = tk.Menu(menubar,tearoff=0)
menubar.add_cascade(label='file',menu=filemenubar)
# 在File选项中,增加New、Open、Save,分隔线、以及Exit
filemenubar.add_command(label='New',command=do_job)
filemenubar.add_command(label='Open',command=do_job)
filemenubar.add_command(label='Save',command=do_job)
filemenubar.add_separator()
filemenubar.add_command(label='exit',command=window.quit)
# 我们来创建File中的Submenu
submenu =tk.Menu(filemenubar)
filemenubar.add_cascade(label='Import',menu=submenu,underline=0)
submenu.add_command(label='Save',command=do_job)
#显示menubar
window.config(menu=menubar)
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('300x300')
tk.Label(window,text='on the window').pack()
frm=tk.Frame(window)
frm.pack()
frm_l=tk.Frame(frm,)
frm_r=tk.Frame(frm)
frm_l.pack(side='left')
frm_r.pack(side='right')
tk.Label(frm_l,text='on the frm_l1').pack()
tk.Label(frm_l,text='on the frm_l2').pack()
tk.Label(frm_r,text='on the frm_r1').pack()
window.mainloop()
#引入弹窗信息
from tkinter import messagebox
# showinfo
tk.messagebox.showinfo(title='Hi', message='hahahaha')
# showwarning
tk.messagebox.showwarning(title='Hi', message='nononono')
# showerror
tk.messagebox.showerror(title='Hi', message='No!! never')
# askquestion返回是字符串,即返回的是‘yes’或者‘no’,可通过print打印出来以更好的理解
print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) # return 'yes' , 'no'
# askyesno
print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) # return True, False
# asktrycancel
print(tk.messagebox.asktrycancel(title='Hi', message='hahahaha')) # return True, False
# askokcancel
print(tk.messagebox.askokcancel(title='Hi', message='hahahaha')) # return True, False
# askyesnocancel
print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None
# pack
# side属性来决定所放置的位置,共有top、bottom、left、right
tk.Label(window,text='1').pack(side='top')
tk.Label(window,text='1').pack(side='bottom')
tk.Label(window,text='1').pack(side='left')t
tk.Label(window,text='1').pack(side='right')
# grid
for i in range(4):
for j in range(3):
tk.Label(window,text=1).grid(row=i,column=j,padx=10,pady=10)
# place
tk.Label(window,text=1).place(x=20,y=10,anchor='nw')