ch7_1.py
# ch7_1.py
from tkinter import *
def printSelection():
num = var.get()
if num == 1:
label.config(text="你是男生")
else:
label.config(text="你是女生")
root = Tk()
root.title("ch7_1") # 设置窗口标题
var = IntVar() # 选项按钮绑定的变量
var.set(1) # 默认选项是男生
# var.set(0) # 可以设置初始不默认任何选项
label = Label(root,text="这是预设,尚未选择",bg="lightyellow",width=30)
label.pack()
rbman = Radiobutton(root,text="男生", # 男生选项按钮
variable=var,value=1, # value用来区分所选择的选项按钮
command=printSelection) # 女生选项按钮
rbman.pack()
rbwoman = Radiobutton(root,text="女生",
variable=var,value=2,
command=printSelection)
rbwoman.pack()
root.mainloop()
ch7_2.py
# ch7_2.py
from tkinter import *
def printSelection():
label.config(text="你是"+var.get())
root = Tk()
root.title("ch7_2") # 设置窗口标题
var = StringVar() # 选项按钮绑定的变量
var.set("男生") # 默认选项是男生
# var.set("生") # 默认选项是空
# var.set(0) # 可以设置初始不默认任何选项
label = Label(root,text="这是预设,尚未选择",bg="lightyellow",width=30)
label.pack()
rbman = Radiobutton(root,text="男生-吴彦祖", # 男生选项按钮
variable=var,value="男生", # value用来区分所选择的选项按钮
command=printSelection) # 女生选项按钮
rbman.pack()
rbwoman = Radiobutton(root,text="女生-刘亦菲",
variable=var,value="女生",
command=printSelection)
rbwoman.pack()
root.mainloop()
ch7_3.py
# ch7_2.py
from tkinter import *
def printSelection():
print(cities[var.get()],"被选中!!!")
root = Tk()
root.title("ch7_3") # 设置窗口标题
cities = { 0:"北京" ,1:"上海",
2:"广州",3:"深圳",
4:"杭州",5:"苏州",
}
var = IntVar() # 选项按钮绑定的变量
var.set(-1) # 默认选项
# var.set(0) # 默认选项
label = Label(root,text="选择最喜欢的城市",
fg="blue",bg="lightyellow",width=30)
label.pack()
for val,city in cities.items():
Radiobutton(
root,
text=city,
variable=var,value=val,
command=printSelection
).pack()
root.mainloop()
ch7_4.py
# ch7_4.py
from tkinter import *
def printSelection():
print(cities[var.get()],"被选中!!!")
root = Tk()
root.title("ch7_4") # 设置窗口标题
cities = { 0:"北京" ,1:"上海",
2:"广州",3:"深圳",
4:"杭州",5:"苏州",
}
var = IntVar() # 选项按钮绑定的变量
var.set(0) # 默认选项
# var.set(0) # 默认选项
label = Label(root,text="选择最喜欢的城市",
fg="blue",bg="lightyellow",width=30)
label.pack()
for val,city in cities.items():
Radiobutton(
root,
text=city,
indicatoron = 0, # 用盒子取代选项按钮
width=30,
variable=var,value=val,
command=printSelection
).pack()
root.mainloop()
ch7_5.py
# ch7_5.py
from tkinter import *
def printSelection():
label.config(text="You have chosen "+var.get()+".gif.")
root = Tk()
root.title("ch7_5")
img_chuckle = PhotoImage(file="chuckle.gif")
img_shock = PhotoImage(file="shock.gif")
img_snap = PhotoImage(file="snap.gif")
var = StringVar() # 选项按钮绑定的变量
var.set(" ") # 默认全不选
# var.set("") # 默认全选
label = Label(root,text="This is a defult option, please choose...",bg="lightyellow",width=35)
label.pack()
rb_chuckle = Radiobutton(root,image=img_chuckle,
variable=var,value="chuckle",
command=printSelection)
rb_chuckle.pack()
rb_shock = Radiobutton(root,image=img_shock,
variable=var,value="shock",
command=printSelection)
rb_shock.pack()
rb_snap = Radiobutton(root,image=img_snap,
variable=var,value="snap",
command=printSelection)
rb_snap.pack()
root.mainloop()
ch7_6.py
# ch7_6.py
from tkinter import *
def printSelection():
label.config(text="You have chosen "+var.get()+".gif.")
root = Tk()
root.title("ch7_6")
img_chuckle = PhotoImage(file="chuckle.gif")
img_shock = PhotoImage(file="shock.gif")
img_snap = PhotoImage(file="snap.gif")
var = StringVar() # 选项按钮绑定的变量
var.set(" ") # 默认全不选
# var.set("") # 默认全选
label = Label(root,text="This is a defult option, please choose...",bg="lightyellow",width=35)
label.pack()
rb_chuckle = Radiobutton(root,image=img_chuckle,
text="Gakki chuckles.\t",compound=RIGHT,
variable=var,value="chuckle",
command=printSelection)
rb_chuckle.pack()
rb_shock = Radiobutton(root,image=img_shock,
text="Gakki shocks.\t",compound=RIGHT,
variable=var,value="shock",
command=printSelection)
rb_shock.pack()
rb_snap = Radiobutton(root,image=img_snap,
text="Gakki snaps her fingers.\t",compound=RIGHT,
variable=var,value="snap",
command=printSelection)
rb_snap.pack()
root.mainloop()
ch7_7.py
# ch7_7.py
from tkinter import *
root = Tk()
root.title("ch7_7")
lab = Label(root,text="请选择喜欢的运动",fg="blue",bg="lightyellow",width=30)
lab.grid(row=0)
var1 = IntVar()
cbtnNFL = Checkbutton(root,text="美式足球",variable=var1)
cbtnNFL.grid(row=1,sticky=W) # 左对齐
var2 = IntVar()
cbtnMLB = Checkbutton(root,text="棒球",variable=var2)
cbtnMLB.grid(row=2,sticky=W) # 左对齐
var3 = IntVar()
cbtnNBA = Checkbutton(root,text="篮球",variable=var3)
cbtnNBA.grid(row=3,sticky=W) # 左对齐
root.mainloop()
ch7_8.py
# ch7_8.py
from tkinter import *
def printInfo():
selection = ""
# print("before iteration:")
# print(selection)
for i in checkboxes.keys(): # 检查此字典的关键字,同: for i in checkboxes:
if checkboxes[i].get() == True: # 若被选中则执行
selection = selection + sports[i] + "\t"
# print(selection)
# print("after iteration:")
print(selection)
root = Tk()
root.title("ch7_8") # 窗口标题
label = Label(root,text="请选择喜欢的运动",fg="blue",bg="lightyellow",width=30)
label.grid(row=0)
sports = {0:"美式足球",1:"棒球",2:"篮球",3:"网球",4:"足球",5:"游泳"} # 运动项目构成的字典
checkboxes = {} # 字典,存放被选取项目的BooleanVar变量
for i in range(len(sports)): # 根据运动字典建立复选框
checkboxes[i] = BooleanVar() # 建立布尔变量对象
Checkbutton(root,text=sports[i],
variable=checkboxes[i]).grid(row=i+1,sticky=W)
btn = Button(root,text="确定",width=10,command=printInfo)
btn.grid(row=i+2)
# btn.grid()
root.mainloop()
ch7_9.py
# ch7_9.py
from tkinter import *
# 以下是callback方法
def selAll(): # 选取全部字符串
entry.select_range(0,END)
def deSel(): # 取消选取
entry.select_clear()
def clr(): # 删除文字
entry.delete(0,END)
def read_only():
print("调用read_only函数中...",var.get())
if var.get() == True:
entry.config(state=DISABLED) # 设为DISABLED 不可使用
else:
entry.config(state=NORMAL) # 设为NORMAL
root = Tk()
root.title("ch7_9") # 窗口标题
# 以下row=0建立Entry
entry = Entry(root)
entry.grid(row=0,column=0,columnspan=4,
padx=5,pady=5,sticky=W)
# 以下row=1建立Button
btnSel = Button(root,text="全选",command=selAll)
btnSel.grid(row=1,column=0,padx=5,pady=5,sticky=W)
btnDesel = Button(root,text="取消选取",command=deSel)
btnDesel.grid(row=1,column=1,padx=5,pady=5,sticky=W)
btnClr = Button(root,text="删除",command=clr)
btnClr.grid(row=1,column=2,padx=5,pady=5,sticky=W)
btnQuit = Button(root,text="结束程序",command=root.destroy)
btnQuit.grid(row=1,column=3,padx=5,pady=5,sticky=W)
# 以下row=2建立Checkboxes
var = BooleanVar()
var.set(False)
# var.set(True)
# read_only()
chkReadOnly = Checkbutton(root,text="只读",variable=var,
command=read_only)
chkReadOnly.grid(row=2,column=0)
root.mainloop()