
1.按钮
tkBtton.py
import tkinter root = tkinter.Tk() btn1 = tkinter.Button(root, anchor=tkinter.E,\ text='Button1', width=40, height=5) btn1.pack() btn2 = tkinter.Button(root, \ text='Button2', bg='blue') btn2.pack() btn3 = tkinter.Button(root, \ text='Button3', width=14, height=1) btn3.pack() btn4 = tkinter.Button(root, \ text='Button4', width=60, height=5, state=tkinter.DISABLED) btn4.pack() root.mainloop()
2.标签
tkLabel.py
import tkinter root = tkinter.Tk() lbl1 = tkinter.Label(root, anchor=tkinter.E, bg='blue', fg='red', text='Python', width=30, height=5) lbl1.pack() lbl2 = tkinter.Label(root, text='Python GUI\nTkinter', justify=tkinter.LEFT, width=30, height=5) lbl2.pack() lbl3 = tkinter.Label(root, text='Python GUI\nTkinter', justify=tkinter.RIGHT, width=30, height=5) lbl3.pack() lbl4 = tkinter.Label(root, text='Python GUI\nTkinter', justify=tkinter.CENTER, width=30, height=5) lbl4.pack() root.mainloop()
3.单选框、复选框
tkCheck.py
import tkinter root = tkinter.Tk() r = tkinter.StringVar() r.set(') radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio1') radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio2') radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio3') radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio4') radio.pack() c = tkinter.IntVar() c.set(1) check = tkinter.Checkbutton(root, text='CheckButton', variable=c, onvalue=1, offvalue=2) check.pack() root.mainloop() print(r.get()) print(c.get())
4.单选框、复选框的平坦样式
tkRCButton.py
import tkinter root = tkinter.Tk() r = tkinter.StringVar() r.set(') radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio1', indicatoron=0) radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio2', indicatoron=0) radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio3', indicatoron=0) radio.pack() radio = tkinter.Radiobutton(root, variable=r, value=', text='Radio4', indicatoron=0) radio.pack() c = tkinter.IntVar() c.set(1) check = tkinter.Checkbutton(root, text='CheckButton', variable=c, onvalue=1, offvalue=2, indicatoron=0) check.pack() root.mainloop() print(r.get()) print(c.get())
5.文本框
tkEntry.py
import tkinter root = tkinter.Tk() entry1 = tkinter.Entry(root, show='*' ) entry1.pack() entry2 = tkinter.Entry(root, show=') entry2.pack() entry3 = tkinter.Entry(root, bg='red', fg='blue') entry3.pack() entry4 = tkinter.Entry(root, selectbackground='red',\ selectforeground='gray') entry4.pack() entry5 = tkinter.Entry(root, state=tkinter.DISABLED) entry5.pack() edit1 = tkinter.Text(root, selectbackground='red', selectforeground='gray') edit1.pack() root.mainloop()