'''
主题:tk学习
内容:Menu 菜单
时间:2021/10/20
'''
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
counter = tk.IntVar()
counter.set(0)
l=tk.Label(window,textvariable=counter,bg='yellow',width='20')
l.pack()
def do_job():
#global counter
counter.set(counter.get()+1)
#l.config(text='do '+str(counter))
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar,tearoff=0) # 不可分割
menubar.add_cascade(label='File',menu=filemenu) #cascade:小瀑布、倾斜;这里表示多级菜单
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,tearoff=0)
filemenu.add_cascade(label='import',menu=submenu,underline=0)
submenu.add_command(label='submenu1',command=do_job)
window.config(menu=menubar)
window.mainloop()
'''
主题:tk学习
内容:frame 框架
时间:2021/10/20
'''
import tkinter as tk
window = tk.Tk()
window .title('my window')
window.geometry('250x200')
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 frame_left1').pack()
tk.Label(frm_l,text='on the frame_left2').pack()
tk.Label(frm_r,text='on the frame_right1').pack()
window.mainloop()
'''
主题:tk学习
内容:messagebox 弹窗
时间:2021/10/20
'''
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window .title('my window')
window.geometry('200x200')
def hit_me():
e = tk.messagebox.showinfo(title='show',message='info') #返回字符串 ok
f = tk.messagebox.showwarning(title='show',message='warning') #返回字符串 ok
g = tk.messagebox.showerror(title='show',message='error') #返回字符串 ok
a = tk.messagebox.askquestion(title='show',message='askquestion') # 返回字符串 'yes','no'
b = tk.messagebox.askyesno(title='show',message='askyesno') # 返回布尔值 TRUE,FALSE
c = tk.messagebox.askokcancel(title='show',message='askokcancel') # 返回布尔值 TRUE,FALSE
if b==True:
print('!!!')
tk.Button(window,text='hit me',command=hit_me).pack()
window.mainloop()
'''
主题:tk学习
内容:pack side place 放置位置
时间:2021/10/20
'''
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window .title('my window')
window.geometry('200x200')
# tk.Label(window,text=1).pack(side='top')
# tk.Label(window,text=1).pack(side='bottom')
# tk.Label(window,text=1).pack(side='left')
# tk.Label(window,text=1).pack(side='right')
# grid按格子放置,例如现在四行三列:
for i in range(4):
for j in range(3):
tk.Label(window,text=2).grid(row=i,column=j,padx=10,pady=10)#padx、pady内部拓展,ipadx、ipady一样
# 精准放置
tk.Label(window,text=1).place(x=1,y=1,anchor='nw') # anchor 锚点 图片相对于xy坐标放上去的点
window.mainloop()
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import pickle
window = tk.Tk()
window.title('Login')
window.geometry('450x350')
# welcome image
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome.gif')
image = canvas.create_image(0,0, anchor='nw', image=image_file)
canvas.pack(side='top')
# user information
tk.Label(window, text='User name: ').place(x=50, y= 220)
tk.Label(window, text='Password: ').place(x=50, y= 260)
var_usr_name = tk.StringVar()
var_usr_name.set('example@python.com')
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=220)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=260)
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name)
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again.')
else:
is_sign_up = tk.messagebox.askyesno('Welcome',
'You have not signed up yet. Sign up today?')
if is_sign_up:
usr_sign_up()
def usr_sign_up():
def sign_to_Mofan_Python():
np = new_pwd.get()
npf = new_pwd_/confirm/i.get()
nn = new_name.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
if np != npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
elif nn in exist_usr_info:
tk.messagebox.showerror('Error', 'The user has already signed up!')
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.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('Sign up window')
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_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_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_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_/confirm/i, show='*')
entry_usr_pwd_/confirm/i.place(x=150, y=90)
btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)
# login and sign up button
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=170, y=300)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=270, y=300)
window.mainloop()