Python tkinter 从入门到放弃(3)

目录:Python tkinter 从入门到放弃(1)_BBBBBBBBBBBBBBBUG的博客-CSDN博客

文章目录

一.grid()的使用

1.堆叠用法

2.合并用法

                                               欢迎补充

附tkinter grid函数源码(注释已翻译):

一.grid()的使用

在学习grid之前,先引用一个知识点:窗口和坐标系;如图所示:

注:窗口的x,y只有正半轴,所以控件位置(x,y)任何一个小于0,控件就会显示不全或不显示

注意:grid()与pack()函数不一样,grid()函数内必须有参数,且在一个代码文件里不能同时存在grid

           和pack,不然会报错

1.堆叠用法

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口

num = 1
for i in range(5):
         for j in range(5):
                  label_1 = tk.Label(root,text = f"标签{num}   ")
                  label_1.grid(row = i,column = j)
                                 #行      #列
                  num += 1

root.mainloop()    #消息循环

运行:

 它和pack的区别:pack从窗口坐标((长-标签长)/2,0)开始的,但grid从(0,0)开始

grid(row = 横排个数,column = 竖排个数)

如图所示:

假如我想找”标签17“的row和column值,就可以按照这个表:

取得"标签17" 的row和column值为(1,3)

注意:row和column用法不会随窗口变化而变化,它只跟所堆叠的控件有关

2.合并用法

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口

num = 1
for i in range(5):
         for j in range(5):
                  label_1 = tk.Label(root,text = f"标签{num}   ")
                  label_1.grid(row = i,column = j,rowspan = 5,columnspan = 5)
                  num += 1

root.mainloop()    #消息循环

 运行:

 grid(rowspan = 在x轴上合并控件的个数,columnspan = 在y轴上合并控件的个数)

注意:rowspan和columnspan 都是正整数,不然会报错

(其实作者也不知道这到底有嘛用[dogs])

其他函数如:ipadx,ipady,pads,pady和pack()一样

Python tkinter 从入门到放弃(2)_BBBBBBBBBBBBBBBUG的博客-CSDN博客

                                               欢迎补充

附tkinter grid函数源码(注释已翻译):

class Grid:
    """几何管理器网格。在每个小部件中使用grid_*方法的基类."""
    # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)

    def grid_configure(self, cnf={}, **kw):
        """Position a widget in the parent widget in a grid. Use as options:
        column=number - use cell identified with given column (starting with 0)
        columnspan=number - this widget will span several columns
        in=master - use master to contain this widget
        in_=master - see 'in' option description
        ipadx=amount - add internal padding in x direction
        ipady=amount - add internal padding in y direction
        padx=amount - add padding in x direction
        pady=amount - add padding in y direction
        row=number - use cell identified with given row (starting with 0)
        rowspan=number - this widget will span several rows
        sticky=NSEW - if cell is larger on which sides will this
                      widget stick to the cell boundary
        """
        '''
        在网格中的父窗口中放置一个窗口小部件。用作选项: column = number-use cell identified 
        with given column (starting with 0) columnspan = number-this widget will span
        several columns in = master-use master to contain this widget in _ = master-see‘
        in’option description ipadx = amount-add internal padding in x direction ipady = 
        amount-add internal padding in y direction padx = amount-add padding in x 
        direction padx direction y = amount-add padding in x direction padx = amount-add 
        padding in y direction cell row = number-use widget identified with given row 
        (starting with 0) span = number-this will span = n- 如果单元格更大,那么哪一侧会粘在
        单元格小部件的边界上
        '''
        self.tk.call(
              ('grid', 'configure', self._w)
              + self._options(cnf, kw))

    grid = configure = config = grid_configure
    bbox = grid_bbox = Misc.grid_bbox
    columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure

    def grid_forget(self):
        """映射这个小部件"""
        self.tk.call('grid', 'forget', self._w)

    forget = grid_forget

    def grid_remove(self):
        """取消这个小部件的映射,但记住网格选项。"""
        self.tk.call('grid', 'remove', self._w)

    def grid_info(self):
        """返回关于在网格中定位此小部件的选项的信息。"""
        d = _splitdict(self.tk, self.tk.call('grid', 'info', self._w))
        if 'in' in d:
            d['in'] = self.nametowidget(d['in'])
        return d

    info = grid_info
    location = grid_location = Misc.grid_location
    propagate = grid_propagate = Misc.grid_propagate
    rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
    size = grid_size = Misc.grid_size
    slaves = grid_slaves = Misc.grid_slaves

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值