python3.6 Tkinter 入门

来源:互联网 发布:炉石淘宝买卡背 编辑:程序博客网 时间:2024/05/18 18:43

接python2.7的后续

05Radiobutton

#coding=utf-8import tkinter as tkwindow = 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-8import tkinter as tkwindow = 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)                                                                        #像素的lengths = 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-8import tkinter as tkwindow = 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 tkwindow = 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,80line = 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 tkwindow = tk.Tk()window.title('my window')window.geometry('200x200')l = tk.Label(window,text='',bg='yellow')l.pack()counter = 0def do_job():    global  counter    l.config(text='do '+str(counter))    counter+=1menubar = 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 tkwindow = 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-8import tkinter as tkfrom 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 Falsetk.Button(window,text='hit me',command=hit_me).pack()window.mainloop()

12 pack,,grid,,place

#coding=utf-8import 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) #ipadxtk.Label(window,text=1).place(x=10,y=100,anchor='se')window.mainloop()


13 login

#coding=utf-8import tkinter as tkfrom tkinter import messageboxwindow = tk.Tk()window.title('Welcome to Python')window.geometry('450x300')#welcome imagecanvas = 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 informationtk.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 updef usr_login():    passdef usr_sign_up():    passbtn_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-8import tkinter as tkfrom tkinter import messageboximport pickle   #pickle 存放数据window = tk.Tk()window.title('Welcome to Python')window.geometry('450x300')#welcome imagecanvas = 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 informationtk.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 updef 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 0