《Python GUI设计 tkinter菜鸟编程》配套代码 第07章 选项按钮与复选框

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()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值