Checkbutton
import tkinter as tk
window = tk.Tk()
window.title("CheckButton")
window.geometry('300x200')
label = tk.Label(window,bg='yellow',width=20,height=2)
label.pack()
def print_selection():
if var1.get() == 1 and var2.get() == 0:
label.config(text='I only love Python!')
elif var1.get() == 0 and var2.get() == 1:
label.config(text='I only love C++')
elif var1.get() == 1 and var2.get() == 1:
label.config(text='I love both')
else:
label.config(text="I don't love either")
var1 = tk.IntVar()
var2 = tk.IntVar()
cb1 = tk.Checkbutton(window,text='Python',variable=var1,onvalue=1,offvalue=0,command=print_selection)
cb2 = tk.Checkbutton(window,text='C++',variable=var2,onvalue=1,offvalue=0,command=print_selection)
cb1.pack()
cb2.pack()
window.mainloop()
Menubar
import tkinter as tk
window = tk.Tk()
window.title('MenuBar')
window.geometry('300x150')
label = tk.Label(window,bg='yellow')
label.pack()
counter = 0
def do_job():
global counter
label.config(text='do ' + str(counter))
counter += 1
menubar = tk.Menu(window)
filemenu = tk.Menu(menubar,tearoff=0)
editmenu = tk.Menu(menubar,tearoff=0)
menubar.add_cascade(label='File',menu=filemenu)
menubar.add_cascade(label='Edit',menu=editmenu)
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.add_command(label='Copy',command=do_job)
editmenu.add_command(label='Cut',command=do_job)
editmenu.add_command(label='Paste',command=do_job)
submenu = tk.Menu(filemenu,tearoff=0)
filemenu.add_cascade(label='Import',menu=submenu)
submenu.add_command(label='Tkinter',command=do_job)
window.config(menu=menubar)
window.mainloop()
Frame
import tkinter as tk
window = tk.Tk()
window.title('Frame')
window.geometry('300x300')
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 thr frm_r').pack()
window.mainloop()
Messagebox
import tkinter as tk
import tkinter.messagebox
window = tk.Tk()
window.title('messageBox')
window.geometry('300x300')
def showinfor():
tk.messagebox.showinfo(title='Hi', message='hhhhh')
def showwarning():
tk.messagebox.showwarning(title='Hi', message='hhhhh')
def showerror():
tk.messagebox.showerror(title='Hi', message='hhhhh')
def askquestion():
tk.messagebox.askquestion(title='Hi', message='hhhhh')
def askyesno():
tk.messagebox.askyesno(title='Hi', message='hhhhh')
def askokcancel():
tk.messagebox.askokcancel(title='Hi', message='hhhhh')
def asktrycancel():
tk.messagebox.askretrycancel(title='Hi', message='hhhhh')
tk.Button(window,text='showinfor',command=showinfor).pack()
tk.Button(window,text='showwarning',command=showwarning).pack()
tk.Button(window,text='showerror',command=showerror).pack()
tk.Button(window,text='askquestion',command=askquestion).pack() #return 'yes' or 'no'
tk.Button(window,text='askyesno',command=askyesno).pack() #return True or False
tk.Button(window,text='asktrycancle',command=asktrycancel).pack() #return True or False
tk.Button(window,text='askokcancle',command=askokcancel).pack() #return True or False
window.mainloop()