按照视频顺序来的,照着打的代码
#Label & Button import tkinter as tk window=tk.Tk() #创建窗口 window.title('1200') window.geometry('400x300') var=tk.StringVar() #一个变量 l=tk.Label(window, textvariable=var,bg='green',fg='red',font=('Arial',12),width=30,height=2) #创建一个label l.pack() #把label加进窗口 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()
#Entry & Text window=tk.Tk() window.title('1200') window.geometry('400x300') e=tk.Entry(window,show=None) e.pack() t=tk.Text(window,height=2) def insert_point(): var=e.get() t.insert('insert',var) def insert_end(): var=e.get() t.insert('end',var) b1=tk.Button(window,text='insert point',width=15,height=2,command=insert_point) b1.pack() b2=tk.Button(window,text='insert end',width=15,height=2,command=insert_end) b2.pack() t.pack() window.mainloop()
#Listbox window=tk.Tk() window.title('1200') window.geometry('400x300') var1=tk.StringVar() l=tk.Label(window,bg='yellow',width=30,height=2,textvariable=var1) l.pack() def print_selection(): value=lb.get(lb.curselection()) var1.set(value) 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.insert(1,'second') ''' lb.pack() window.mainloop()
#Radiobutton window=tk.Tk() window.title('1200') window.geometry('400x300') var=tk.StringVar() l=tk.Label(window,bg='yellow',width=30,height=2,text='empty') l.pack() def print_selection(): l.config(text='you have select '+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()
#Scale window=tk.Tk() window.title('1200') window.geometry('400x300') l=tk.Label(window,bg='yellow',width=30,height=2,text='empty') l.pack() def print_selection(v): l.config(text='you have selected '+v) s=tk.Scale(window,label='try me',from_=5,to=11,orient=tk.HORIZONTAL,length=300, showvalue=True,tickinterval=1,resolution=0.01,command=print_selection) s.pack() window.mainloop()
#Checkbutton window=tk.Tk() window.title('1200') window.geometry('400x300') l=tk.Label(window,bg='yellow',width=30,height=2,text='empty') l.pack() def print_selection(): if (var1.get()==1)and(var2.get()==0): l.config(text='I only love Python') elif (var1.get()==0)and(var2.get()==1): l.config(text='I only love C++') elif (var1.get()==1)and(var2.get()==1): l.config(text='I love both') else: l.config(text='I do not love either') 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()
#Canvas window=tk.Tk() #window=tk.Toplevel() #如果报错,改成这一行 window.title('my window') window.geometry('600x600') canvas=tk.Canvas(window,bg='blue',height=400,width=600) image_file=tk.PhotoImage(file='1.gif')#只能打开gif 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+30,y0+30,x1+30,y1+30,star=0,extent=180,fill='green') rect=canvas.create_rectangle(100,30,100+20,30+20) ''' rect=canvas.create_rectangle(100,30,100+20,30+20) canvas.pack() def moveit(): canvas.move(rect,0,2) b=tk.Button(window,text='move',width=15,height=2,command=moveit).pack() window.mainloop()
#Menubar window=tk.Tk() #window=tk.Toplevel() #如果报错,改成这一行 window.title('my window') window.geometry('600x600') l=tk.Label(window,bg='yellow',text='') 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) filemenu.add_cascade(label='Import',menu=submenu,underline=0) submenu.add_command(label='Submenu1',command=do_job) window.config(menu=menubar) window.mainloop()
#Frame window=tk.Tk() #window=tk.Toplevel() #如果报错,改成这一行 window.title('my window') window.geometry('400x300') tk.Label(window,text='on the window').pack(side='top') frm=tk.Frame(window).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_l1').pack() window.mainloop()
#messagebox window=tk.Tk() from tkinter import messagebox #否则会报错 #window=tk.Toplevel() #如果报错,改成这一行 window.title('my window') window.geometry('400x300') def hit_me(): #messagebox.showinfo(title='Hi',message='hahaha') #messagebox.showwarning(title='Hi',message='nonono') #messagebox.showerror(title='Hi',message='No!!!') #print(messagebox.askquestion(title='Hi',message='hahaha')) #return yes or no #print(messagebox.askyesno(title='Hi',message='hahaha')) #return True or false #print(messagebox.asktrycancel(title='Hi',message='hahaha')) #同上 print(messagebox.askokcancel(title='Hi',message='hahaha')) #同上 tk.Button(window,text='hit me',command=hit_me).pack() window.mainloop()
#pack grid place window=tk.Tk() window.title('my window') window.geometry('400x300') ''' for i in range(4): for j in range(3): tk.Label(window,text=1).grid(row=i,column=j,padx=10,pady=10) ''' ''' 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') ''' tk.Label(window,text=1).place(x=10,y=100,anchor='nw') window.mainloop()