python3.6 Tkinter 入门

接python2.7的后续

05Radiobutton

#coding=utf-8
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())  # lable中的参数都是可以改的

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()

06 Scale

#coding=utf-8
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)
l.pack()
def print_selection(v):
    l.config(text='you have selected'+v)
                                                                        #像素的length
s = tk.Scale(window,label='try me',from_=5,to=11,orient=tk.HORIZONTAL,length=200,
             showvalue=1,tickinterval=3,resolution=0.01,command=print_selection)
            #存在默认的传入值v
            #showvalue显示数字 tickinterval为分割的份数 resolution 保留的小数位数
s.pack()
window.mainloop()

07 Checkbutton

#coding=utf-8
import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('200x200')

l = tk.Label(window,bg='yellow',width=20,text='empty')
l.pack()

#checkbutton 可以多选

def print_selection():
    print(type(var1.get()))
    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')
    elif (var1.get() == 1)&(var2.get() ==1):
        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()

08 Canvas

#coding=utf-8
#画布,画一些东西,或者是添加图片,可以对他们进行 改变操作(如大小,位置等)
import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('400x200')

canvas = tk.Canvas(window,bg='blue',height=100,width=400)
image_file = tk.PhotoImage(file='1.jpg')
    # anchor = e,s,w,n,nw,ne,se,sw  表示以哪个店来作为坐标确定的基点
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,fill='black',width=10)
oval = canvas.create_oval(x0,y0,x1,y1,fill='red') #圆形
arc = canvas.create_arc(x0+30,y0+30,x1+30,y1+30,start=0,extent=180,width=5) #扇形
rect = canvas.create_rectangle(100,30,120,30+20,width=5) #正方形
canvas.pack()

def moveit():
                #移动的x ,y
    canvas.move(rect,0,2)
b = tk.Button(window,text='move',command=moveit)
b.pack()
window.mainloop()


09 Menubar

#coding=utf-8
#菜单 ,多级栏目
import tkinter as tk

window = tk.Tk()
window.title('my window')
window.geometry('200x200')

l = tk.Label(window,text='',bg='yellow')
l.pack()

counter = 0
def do_job():
    global  counter
    l.config(text='do '+str(counter))
    counter+=1

menubar = tk.Menu(window)
# tearoff=
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)
editmenu.add_separator()  #画一条线
editmenu.add_command(label='Exit',command=window.quit)

submenu = tk.Menu(filemenu,tearoff=0)

filemenu.add_cascade(label='Import',menu=submenu)
submenu.add_command(label='Submenu1',command=do_job)

window.config(menu=menubar) #将menubar 加到 window上

window.mainloop()


10 Frame

#coding=utf-8
#框架 ,进行界面内容的布局
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')

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_r').pack()

window.mainloop()

11 Messagebox

#coding=utf-8
import tkinter as tk
from tkinter import messagebox  #不加这句话没有这个方法,不知为啥

window = tk.Tk()
window.title('my window')
window.geometry('200x200')

def hit_me():
    # tk.messagebox.showinfo(title='Hi',message='hahahahha!')
    # tk.messagebox.showwarning(title='Hi',message='nononono')
    # tk.messagebox.showerror(title='Hi',message='HaHaHa')
    # print(tk.messagebox.askquestion(title='Hi',message='hahahahahah')) # return yes or no
    # print(tk.messagebox.askyesno(title='Hi',message='hahahahahah')) # return True or False
    print(tk.messagebox.askokcancel(title='Hi',message='hahahahahah')) # return True or False
tk.Button(window,text='hit me',command=hit_me).pack()

window.mainloop()

12 pack,,grid,,place

#coding=utf-8
import tkinter as tk
# 图片或者部件 防止位置的三种方法

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')


# for i in range(4):
#     for j in range(3):
#         # tk.Label(window,text=1).grid(row=i,column=j,padx=10,pady=10) #padx 间隔
#         tk.Label(window,text=1).grid(row=i,column=j,ipadx=10,ipady=10) #ipadx

tk.Label(window,text=1).place(x=10,y=100,anchor='se')

window.mainloop()


13 login

#coding=utf-8
import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.title('Welcome to Python')
window.geometry('450x300')

#welcome image
canvas = tk.Canvas(window,height=200,width=500)
image_file = tk.PhotoImage(file='1.jpg')
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=150)
tk.Label(window,text='Password:').place(x=50,y=190)

var_usr_name = tk.StringVar()
var_usr_name.set('hahahahah@sj.com')
var_usr_pwd = tk.StringVar()
entry_usr_name = tk.Entry(window,textvariable=var_usr_name)
entry_usr_name.place(x=160,y=150)

entry_usr_pwd = tk.Entry(window,textvariable=var_usr_pwd,show='*')
entry_usr_pwd.place(x=160,y=190)

#login and sign up
def usr_login():
    pass
def usr_sign_up():
    pass
btn_login = tk.Button(window,text='Login',command=usr_login)
btn_login.place(x=170,y=230)
btn_sign_up = tk.Button(window,text='Sign up',command=usr_sign_up)
btn_sign_up.place(x=270,y=230)

window.mainloop()


14 login 

#coding=utf-8
import tkinter as tk
from tkinter import messagebox
import pickle   #pickle 存放数据

window = tk.Tk()
window.title('Welcome to Python')
window.geometry('450x300')

#welcome image
canvas = tk.Canvas(window,height=200,width=500)
image_file = tk.PhotoImage(file='1.jpg')
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=150)
tk.Label(window,text='Password:').place(x=50,y=190)

var_usr_name = tk.StringVar()
var_usr_name.set('HaHaHaHa@jsdf.com')
var_usr_pwd = tk.StringVar()
entry_usr_name = tk.Entry(window,textvariable=var_usr_name)
entry_usr_name.place(x=160,y=150)

entry_usr_pwd = tk.Entry(window,textvariable=var_usr_pwd,show='*')
entry_usr_pwd.place(x=160,y=190)

#login and sign up
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(title='Welcome',message='You have no sign up yet.Sign up today?')
        if is_sign_up:
            usr_sign_up()
#sign up界面
def usr_sign_up():
    def sign_to_Python():
        np = new_pwd.get()
        npf = new_pwd_confirm.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 successful')
            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,show='*')
    entry_usr_pwd_confirm.place(x=150, y=90)

    btn_comfirm_sign_up = tk.Button(window_sign_up,text='Sign up',command=sign_to_Python)
    btn_comfirm_sign_up.place(x=150,y=130)

btn_login = tk.Button(window,text='Login',command=usr_login)
btn_login.place(x=170,y=230)
btn_sign_up = tk.Button(window,text='Sign up',command=usr_sign_up)
btn_sign_up.place(x=270,y=230)
window.mainloop()




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值