栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python tkinter 从入门到放弃(3)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号