Label标签
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
l = tk.Label(window, text='OMG!This is TK!', bg='blue', font=('Arial', 12), width=15, height=2)
l.pack()
window.mainloop()
Label标签和Button按钮
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg='blue', 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=15, height=2, command=hit_me)
b.pack()
window.mainloop()
Listbox列表
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
var = tk.StringVar()
l = tk.Label(window, bg='yellow', width=16, textvariable=var)
l.pack()
def print_selection():
if lb.curselection():
value = lb.get(lb.curselection())
var.set(value)
else:
var.set("Please select an item!")
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))
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.delete(3)
lb.pack()
window.mainloop()
Radiobutton单选按钮
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
var_group1 = tk.StringVar()
var_group2 = tk.StringVar()
l = tk.Label(window, bg='yellow', width=32, text='Empty')
l.pack()
def print_selection():
l.config(text='Your have selected '+var_group1.get())
r1_group1 = tk.Radiobutton(window, text='Option A', variable=var_group1, value="A", command=print_selection)
r1_group1.pack()
r2_group1 = tk.Radiobutton(window, text='Option B', variable=var_group1, value="B", command=print_selection)
r2_group1.pack()
r3_group1 = tk.Radiobutton(window, text='Option C', variable=var_group1, value="C", command=print_selection)
r3_group1.pack()
r1_group2 = tk.Radiobutton(window, text='Option X', variable=var_group2, value="X", command=print_selection)
r1_group2.pack()
r2_group2 = tk.Radiobutton(window, text='Option Y', variable=var_group2, value="Y", command=print_selection)
r2_group2.pack()
r3_group2 = tk.Radiobutton(window, text='Option Z', variable=var_group2, value="Z", command=print_selection)
r3_group2.pack()
window.mainloop()
Scale滑动条
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
l = tk.Label(window, bg='yellow', width=32, text='Empty')
l.pack()
def print_selection(v):
l.config(text='Your have selected '+ v)
s = tk.Scale(window, label='Name', from_=0, to=10, orient=tk.HORIZONTAL, length=200, showvalue=1, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()
window.mainloop()
Checkbutton多选框
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
l = tk.Label(window, bg='yellow', width=32, 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)
c1.pack()
c2 = tk.Checkbutton(window, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection)
c2.pack()
window.mainloop()
Canvas画布
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
canvas = tk.Canvas(window, bg='yellow', height=320, width=240)
image_file = tk.PhotoImage(file='2.png')
image_file = image_file.subsample(20, 20)
image = canvas.create_image(0, 0, 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 + 50, y0 + 50, x1 + 50, y1 + 50, start=0, extent=180)
rect = canvas.create_rectangle(50, 50, 100, 100)
canvas.pack()
def movement():
canvas.move(rect, 0, 2)
b = tk.Button(window, text='move', command=movement)
b.pack()
window.mainloop()
Menubar 菜单
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
l = tk.Label(window, text='', width=32, bg='yellow')
l.pack()
count = 0
def do_job():
global count
l.config(text='do ' + str(count))
count += 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)
submenu = tk.Menu(filemenu, tearoff=0)
filemenu.add_cascade(label="Import", menu=submenu, underline=0)
submenu.add_command(label='Submenu1', command=do_job)
filemenu.add_separator()
filemenu.add_command(label='Exit', command=window.quit)
window.config(menu=menubar)
window.mainloop()
Frame框架
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
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', bg='red').pack()
tk.Label(frm_l, text='On the frm_l2', bg='green').pack()
tk.Label(frm_l, text='On the frm_l3', bg='blue').pack()
tk.Label(frm_r, text='On the frm_r1', bg='yellow').pack()
window.mainloop()
Messagebox弹窗
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
def hit_me():
print(messagebox.askretrycancel(title='Hi', message='hahahahaha'))
tk.Button(window, text='Hit me', command=hit_me).pack()
window.mainloop()
pack/grid/place 布局方式
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title('My window')
window.geometry('640x480')
tk.Label(window,text=3).place(x=10,y=100,anchor='nw')
window.mainloop()
登录注册案例
import pickle
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title('登录窗口')
window.geometry('640x480')
canvas = tk.Canvas(window, height=200, width=640, background='yellow')
image_file = tk.PhotoImage(file='2.png')
image_file = image_file.subsample(4, 4)
image = canvas.create_image(150, 25, anchor='nw', image=image_file)
canvas.pack(side='top')
tk.Label(window, text='User name: ').place(x=200, y=250)
tk.Label(window, text='Password: ').place(x=200, y=290)
var_user_name = tk.StringVar()
var_user_name.set('example@python.com')
entry_user_name = tk.Entry(window, textvariable=var_user_name)
entry_user_name.place(x=300, y=250)
var_user_pwd = tk.StringVar()
entry_user_pwd = tk.Entry(window, textvariable=var_user_pwd, show='*')
entry_user_pwd.place(x=300, y=290)
def user_login():
user_name = var_user_name.get()
user_pwd = var_user_pwd.get()
try:
with open('users_info.pickle', 'rb') as user_file:
users_info = pickle.load(user_file)
except FileNotFoundError:
with open('users_info.pickle', 'wb') as user_file:
users_info = {'admin': 'admin'}
pickle.dump(users_info, user_file)
if user_name in users_info:
if user_pwd == users_info[user_name]:
messagebox.showinfo('Welcome', 'How are you?' + user_name)
else:
messagebox.showerror('Error!', 'Your password is wrong, try again?')
else:
is_sign_up = messagebox.askyesno('Welcome', 'You have not signed up yet. Sign up today?')
if is_sign_up:
user_sign_up()
btn_login = tk.Button(window, text='Login', command=user_login)
btn_login.place(x=250, y=350)
def user_sign_up():
def sign():
np = new_pwd.get()
npc = new_pwd_confirm.get()
nn = new_name.get()
with open('users_info.pickle', 'rb') as user_file:
exist_users_info = pickle.load(user_file)
if np != npc:
messagebox.showerror('Error', 'Password and confirm password must be the same!')
elif nn in exist_users_info:
messagebox.showerror('Error', 'The user has already signed up!')
else:
exist_users_info[nn] = np
with open('users_info.pickle', 'wb') as user_file:
pickle.dump(exist_users_info, user_file)
messagebox.showinfo('Welcome', 'You have successfully signed up!')
window_sign_up.destroy()
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册界面')
new_name = tk.StringVar()
new_name.set('example@python.com')
tk.Label(window_sign_up, text='User name: ').place(x=10, y=10)
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
entry_new_name.place(x=150, y=10)
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
entry_new_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_new_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
entry_new_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_new_pwd_confirm.place(x=150, y=90)
btn_confirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign)
btn_confirm_sign_up.place(x=150, y=130)
btn_sign_up = tk.Button(window, text='Sign up', command=user_sign_up)
btn_sign_up.place(x=350, y=350)
window.mainloop()