Python tkinter之Grid

1、指定行和列

row 行

column 列

复制代码
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

if __name__ == '__main__':
    pass
    win = tkinter.Tk()  # 窗口
    win.title('南风丶轻语')  # 标题
    screenwidth = win.winfo_screenwidth()  # 屏幕宽度
    screenheight = win.winfo_screenheight()  # 屏幕高度
    width = 300
    height = 100
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置
    Button(win, text='按钮1', bg='red').grid(
            row=0,  # 行数
            column=0  # 列数
            )
    Button(win, text='按钮2', bg='yellow').grid(
            row=0,  # 行数
            column=1  # 列数
            )
    Button(win, text='按钮3', bg='blue').grid(
            row=0,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮4', bg='red').grid(
            row=1,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮5', bg='yellow').grid(
            row=1,  # 行数
            column=3  # 列数
            )
    Button(win, text='按钮6', bg='blue').grid(
            row=1,  # 行数
            column=4  # 列数
            )
    win.mainloop()
复制代码

 2、指定对齐方式

sticky = N + S + E + W # 对齐方式 N/S/E/W,分别代表上/下/右/左

复制代码
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

if __name__ == '__main__':
    pass
    win = tkinter.Tk()  # 窗口
    win.title('南风丶轻语')  # 标题
    screenwidth = win.winfo_screenwidth()  # 屏幕宽度
    screenheight = win.winfo_screenheight()  # 屏幕高度
    width = 300
    height = 100
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置
    Button(win, text='按钮1', bg='red').grid(
            row=0,  # 行数
            column=0,  # 列数
            sticky=E  # 对齐方式 N/S/E/W,分别代表上/下/右/左
            )
    Button(win, text='按钮2', bg='yellow').grid(
            row=0,  # 行数
            column=1  # 列数
            )
    Button(win, text='按钮3', bg='blue').grid(
            row=0,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮4', bg='red', width=10).grid(
            row=1,  # 行数
            column=0  # 列数
            )
    Button(win, text='按钮5', bg='yellow').grid(
            row=1,  # 行数
            column=1  # 列数
            )
    Button(win, text='按钮6', bg='blue').grid(
            row=1,  # 行数
            column=2  # 列数
            )
    win.mainloop()
复制代码

3、指定占几行几列

①rowspan 占的行数

②columnspan 占的列数

③通过sticky来对齐组件,保证填充,类似pack中的expand=True

复制代码
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

if __name__ == '__main__':
    pass
    win = tkinter.Tk()  # 窗口
    win.title('南风丶轻语')  # 标题
    screenwidth = win.winfo_screenwidth()  # 屏幕宽度
    screenheight = win.winfo_screenheight()  # 屏幕高度
    width = 300
    height = 100
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置
    Button(win, text='按钮1', bg='red').grid(
            row=0,  # 行数
            column=0,  # 列数
            columnspan=2,  # 占的列数
            sticky=E + W  # 对齐方式 N/S/E/W,分别代表上/下/右/左
            )
    Button(win, text='按钮3', bg='blue').grid(
            row=0,  # 行数
            column=2  # 列数

            )
    Button(win, text='按钮4', bg='red').grid(
            row=1,  # 行数
            column=0  # 列数
            )
    Button(win, text='按钮5', bg='yellow').grid(
            row=1,  # 行数
            column=1  # 列数
            )
    Button(win, text='按钮6', bg='blue').grid(
            row=1,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮7', bg='yellow').grid(
            row=0,  # 行数
            column=3,  # 列数
            rowspan=2,  # 占的行数
            ipady=10,  # 内间距Y
            sticky=N + S + E + W  # 对齐方式 N/S/E/W,分别代表上/下/右/左
            )
    win.mainloop()
复制代码

 4、内外间距

复制代码
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

if __name__ == '__main__':
    pass
    win = tkinter.Tk()  # 窗口
    win.title('南风丶轻语')  # 标题
    screenwidth = win.winfo_screenwidth()  # 屏幕宽度
    screenheight = win.winfo_screenheight()  # 屏幕高度
    width = 300
    height = 100
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置
    Button(win, text='按钮1', bg='red').grid(
            row=0,  # 行数
            column=0,  # 列数
            padx=5,  # 横坐标外间距
            pady=5,  # 纵坐标外间距
            ipadx=5,  # 横坐标内间距
            ipady=5,  # 纵坐标内间距
            )
    Button(win, text='按钮2', bg='yellow').grid(
            row=0,  # 行数
            column=1  # 列数
            )
    Button(win, text='按钮3', bg='blue').grid(
            row=0,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮4', bg='red').grid(
            row=1,  # 行数
            column=2  # 列数
            )
    Button(win, text='按钮5', bg='yellow').grid(
            row=1,  # 行数
            column=3  # 列数
            )
    Button(win, text='按钮6', bg='blue').grid(
            row=1,  # 行数
            column=4  # 列数
            )
    win.mainloop()
复制代码

 

posted @   南风丶轻语  阅读(666)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示