1、Label标签、Button按钮
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x100')
var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg='green', font=('Arial', 12), width=15, height=2)
l.pack()
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
on_hit = True
var.set('you hit me')
else:
on_hit = False
var.set('')
b = tk.Button(window, text='hit me', width=12, height=2, command=hit_me)
b.pack()
window.mainloop()
2、Entry输入、Text文本
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x300')
e = tk.Entry(window, show='*')
e.pack()
def insert_point():
var = e.get()
t.insert('insert', var)
def insert_end():
var = e.get()
t.insert(2.2, var)
b1 = tk.Button(window, text='insert point', width=12, height=2, command=insert_point)
b1.pack()
b2 = tk.Button(window, text='insert end', width=12, height=2, command=insert_end)
b2.pack()
t = tk.Text(window, height=2)
t.pack()
window.mainloop()
3、Listbox列项表
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x300')
var1 = tk.StringVar()
def print_selection():
value = lb.get(lb.curselection())
var1.set(value)
l = tk.Label(window, bg='yellow', width=4, textvariable=var1)
l.pack()
b1 = tk.Button(window, text='print selection', width=12, height=2, command=print_selection)
b1.pack()
var2 = tk.StringVar()
var2.set((11, 22, 33, 44))
lb = tk.Listbox(window, listvariable=var2)
list_items = [1, 2, 3, 4]
for item in list_items:
lb.insert('end', item)
lb.insert(1, 'first')
lb.insert(2, 'second')
lb.delete(2)
lb.pack()
window.mainloop()
4、Radiobutton单选按钮
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x300')
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()
5、Scale刻度条
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x300')
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_=0, to=10, orient=tk.HORIZONTAL,
length=300, showvalue=1, tickinterval=1, resolution=0.01, command=print_selection)
s.pack()
window.mainloop()
6、Checkbutton多选按钮
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('400x300')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
if (var1.get() == 1) and (var2.get() == 0):
l.config(text='i love only python')
elif (var1.get() == 0) and (var2.get() == 1):
l.config(text='i love only c++')
elif (var1.get() == 0) and (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()
7、Canvas画布、PhotoImage照片图片
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('800x400')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
canvas = tk.Canvas(window, bg='blue', height=200, width=200)
image_file = tk.PhotoImage(file='ins.gif')
image = canvas.create_image(10, 10, anchor='nw', image=image_file)
x0, y0, x1, y1 = 50, 50, 80, 80
line = canvas.create_line(x0, y0, x1, y1)
oval = canvas.create_oval(x0, y0, x1, y1, fill='red')
arc = canvas.create_arc(x0 + 100, y0 + 100, x1 + 100, y1 + 100, start=0, extent=180)
rect = canvas.create_rectangle(x0, y0, x1, y1)
canvas.pack()
def moveti():
canvas.move(rect, 0, 20)
b = tk.Button(window, text='move', command=moveti).pack()
window.mainloop()
8、Menu菜单
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('800x400')
l = tk.Label(window, text='', bg='yellow', width=50)
l.pack()
counter = 0
def do_job():
global counter
l.config(text='do' + str(counter))
counter += 1
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New', command=do_job)
filemenu.add_command(label='Open', command=do_job)
filemenu.add_command(label='Save', command=do_job)
filemenu.add_separator()
filemenu.add_command(label='Exit', command=window.quit)
editmenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='Edit', menu=editmenu)
editmenu.add_command(label='Cut', command=do_job)
editmenu.add_command(label='Copy', command=do_job)
editmenu.add_command(label='Paste', command=do_job)
submenu = tk.Menu(filemenu)
menubar.add_cascade(label='Import', menu=submenu, underline=0)
submenu.add_command(label='Submenu1', command=do_job)
window.config(menu=menubar)
window.mainloop()
9、Frame框架
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('800x400')
l = tk.Label(window, text='on the window', bg='yellow')
l.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', bg='yellow').pack()
tk.Label(frm_l, text='on the frm_l2', bg='blue').pack()
tk.Label(frm_r, text='on the frm_r1', bg='red').pack()
tk.Label(frm_r, text='on the frm_r2', bg='green').pack()
window.mainloop()
10、messagebox消息对话框;
import tkinter as tk
import tkinter.messagebox
window = tk.Tk()
window.title('my window')
window.geometry('800x400')
def hit_me():
print(tk.messagebox.askokcancel(title='Hi', message='hahahahah'))
tk.Button(window, text='hit me', command=hit_me).pack()
window.mainloop()
11、Label标签的place
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('800x400')
tk.Label(window, text=1).place(x=100, y=100, anchor='n')
window.mainloop()