3.1框架
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.title('图片查看器')
# 应用窗口图标
root.iconbitmap('./ico.ico')
#框架
frame1 = LabelFrame(root,text='框架1',padx=5,pady=50)
frame1.pack(padx=100,pady=10)
#按钮
bnt1 = Button(frame1,text='按钮1')
bnt2 = Button(frame1,text='按钮2')
bnt3 = Button(frame1,text='按钮3')
bnt4 = Button(frame1,text='按钮4')
bnt5 = Button(frame1,text='按钮5')
bnt6 = Button(frame1,text='按钮6')
bnt7 = Button(frame1,text='按钮7')
bnt8 = Button(frame1,text='按钮8')
bnt1.grid(row=0,column=0,padx=5,pady=10)
bnt2.grid(row=0,column=1,padx=5,pady=10)
bnt3.grid(row=3,column=0,padx=5,pady=10)
bnt4.grid(row=3,column=1,padx=5,pady=10)
bnt5.grid(row=4,column=0,padx=5,pady=10)
bnt6.grid(row=5,column=1,padx=5,pady=10)
bnt7.grid(row=6,column=0,padx=5,pady=10)
bnt8.grid(row=7,column=1,padx=5,pady=10)
root.mainloop()
3.2.1单选按钮
from tkinter import *
root = Tk()
root.title('单选按钮')
# 应用窗口图标
root.iconbitmap('./ico.ico')
#单选按钮------------------------------------------------------------
r = IntVar()
r.set(2) #默认选项
def clicked(value):
my_lable = Label(root, text='选择'+str(value))
my_lable.pack()
# value= 排列号
Radiobutton(root,text='单选按钮1',variable=r,value=1).pack()
Radiobutton(root,text='单选按钮2',variable=r,value=2).pack()
Radiobutton(root,text='单选按钮3',variable=r,value=3,command=lambda :clicked(r.get())).pack()
Radiobutton(root,text='单选按钮4',variable=r,value=4,command=lambda :clicked(r.get())).pack()
# 按钮
my_lable = Label(root,text='默认选项:'+str(r.get()))
my_lable.pack()
mainloop()
3.2.2单选按钮 for
from tkinter import *
root = Tk()
root.title('单选按钮')
# 应用窗口图标
root.iconbitmap('./ico.ico')
#单选按钮------------------------------------------------------------
MODES = [
('馒头','纯碱馒头'),
('包子', '水煎包'),
('油条', '无铅油条')
]
pizza = StringVar()
pizza.set('馒头')
for text, mode in MODES:
Radiobutton(root,text=text,variable=pizza,value=mode).pack(anchor=W)
def clicked(value):
my_lable = Label(root, text='选择:'+str(value))
my_lable.pack()
# # 按钮
my_button = Button(root,text='选择',command=lambda:clicked(pizza.get()))
my_button.pack()
mainloop()