Python可视化Tkinter进阶-grid布局
时间:2023-04-29 21:37:00
Python可视化Tkinter进阶-grid布局
1、grid布局
1.1、pack布局
1.2、grid布局
grid是python标准库提供的控件布局工具
- column : 设置控件对象显示的列(从0开始)
- row :设置控件对象显示的行(从0开始)
- ipadx :设置控制对象左右内侧距离
- ipady : 设置控件对象上下内部距离
- padx :设置控制对象左右外的距离
- pady : 设置控件对象上下外的距离
- columnspan :设置控件对象的列数
- rowspan : 设置控件对象所占行数
2、简易Base制作64装换工具
# coding:utf-8 import tkinter as tk import base64 import tkinter.messagebox as tm def get_encode(): get_var = te1.get("1.0", "end") en_str = base64.b64encode(get_var.encode("gbk")) en_result = en_str.decode("gbk") tt1.delete("1.0", "end") var2.set("加密结果如下:") tt1.insert("insert", en_result) def get_decode(): get_var = bytes(te1.get("1.0", "end"), encoding="gbk") en_str = base64.b64decode(get_var) en_result = en_str.decode("gbk") tt1.delete("1.0", "end") var2.set("加密结果如下:") tt1.insert("insert", en_result)
def menuCommand():
tm.showinfo("", "功能暂未开放")
def add_menu(name):
main_menu.add_command(label=f"{
name}", command=menuCommand)
window = tk.Tk()
width = 1100
height = 650
window_width = int((window.winfo_screenwidth()-width)/2)
window_height = int((window.winfo_screenheight()-height)/2)
window.title("Base64转换工具")
window.geometry(f"{
width}x{
height}+{
window_width}+{
window_height}")
window.resizable(0, 0)
# window.iconbitmap(r"favicon.ico")
lb1 = tk.Label(window, text="欢迎使用Base64转换工具", font=("宋体", 14), width=110, height=2, relief="groove",
anchor="center", bg="#FDF5E6")
lb2 = tk.Label(window, text="请在下面输入要加密或者解密的内容:", font=("宋体", 14), width=110, height=2, relief="groove",
anchor="w")
te1 = tk.Text(window, width=100, height=10, bg="#FDF5E6", font=("宋体", 14, 'bold'))
bt1 = tk.Button(window, text="Base64加密", font=("宋体", 14), width=10, height=1, relief="raised",
command=get_encode, anchor="e")
bt2 = tk.Button(window, text="Base64解密", font=("宋体", 14), width=10, height=1, relief="raised",
command=get_decode, anchor="w")
bt3 = tk.Button(window, text="关闭", font=("宋体", 14), width=10, height=1,
relief="raised", command=window.quit, anchor="w")
var2 = tk.StringVar()
var2.set("")
te2 = tk.Label(window, textvariable=var2, bg="#FDF5E6", font=("宋体", 14), width=110, height=2,
relief="groove", anchor="w")
tt1 = tk.Text(window, width=100, height=10, bg="#FDF5E6", font=("宋体", 14, 'bold'))
lb3 = tk.Label(window, text="杨小朋制作出品。", bg="#FDF5E6", font=("宋体", 14), width=110, height=2,
anchor="center")
lb4 = tk.Label(window, text="联系方式:2969868321@qq.com ", bg="#FDF5E6", font=("宋体", 14), width=110, height=2,
anchor="e")
lb1.grid(row=0, column=0, columnspan=6)
lb2.grid(row=1, column=0, columnspan=6)
te1.grid(row=2, column=0, columnspan=6)
bt1.grid(row=3, column=2, columnspan=1)
bt2.grid(row=3, column=3, columnspan=1)
bt3.grid(row=3, column=4, columnspan=1)
te2.grid(row=4, column=0, columnspan=6)
tt1.grid(row=5, column=0, columnspan=6)
lb3.grid(row=6, column=0, columnspan=6)
lb4.grid(row=7, column=0, columnspan=6)
main_menu = tk.Menu()
add_menu("文件")
add_menu("修改")
add_menu("保存")
add_menu("撤销")
add_menu("关闭")
window.config(menu=main_menu)
window.mainloop()
效果图
3、tkinter控件的通用的常用属性
- font :设置字体
- width:控件的宽度
- weight:控件的高度(单行文本Entry无高度属性)
- bg:控件的背景颜色
- anchor:设置控件内元素的位置【e右n上s下w左center中,其中eswn分别是(东(east)南(south)西(west)北(north)的首字母)】
值 | 含义 |
---|---|
n | 上中 |
ne | 右上 |
e | 右中 |
se | 右下 |
s | 下中 |
sw | 左下 |
w | 左中 |
nw | 左上 |
center | 正中(默认值) |
# coding:utf-8
import tkinter as tk
window = tk.Tk()
width = 500
height = 240
window_width = (window.winfo_screenwidth()-width)//2
window_height = (window.winfo_screenheight()-height)//2
window.title("Base64转换工具")
window.geometry(f"{
width}x{
height}+{
window_width}+{
window_height}")
window.resizable(0, 0)
tk.Button(window, text="左上", font=("宋体", 14), width=15, height=3, relief="solid", anchor="nw").grid(row=0, column=0)
tk.Button(window, text="上中", font=("宋体", 14), width=15, height=3, relief="solid", anchor="n").grid(row=0, column=1)
tk.Button(window, text="右上", font=("宋体", 14), width=15, height=3, relief="solid", anchor="ne").grid(row=0, column=2)
tk.Button(window, text="左中", font=("宋体", 14), width=15, height=3, relief="solid", anchor="w").grid(row=1, column=0)
tk.Button(window, text="正中", font=("宋体", 14), width=15, height=3, relief="solid", anchor="center").grid(row=1, column=1)
tk.Button(window, text="右中", font=("宋体", 14), width=15, height=3, relief="solid", anchor="e").grid(row=1, column=2)
tk.Button(window, text="左下", font=("宋体", 14), width=15, height=3, relief="solid", anchor="s").grid(row=2, column=1)
tk.Button(window, text="下中", font=("宋体", 14), width=15, height=3, relief="solid", anchor="sw").grid(row=2, column=0)
tk.Button(window, text="右下", font=("宋体", 14), width=15, height=3, relief="solid", anchor="se").grid(row=2, column=2)
window.mainloop()
- relief 控件的边框样式
值 | 含义 |
---|---|
flat | 无边框 |
groove | 凹槽 |
raised | 凸起(默认值) |
ridge | 隆起 |
solid | 实现 |
sunken | 凹陷 |