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

Python tkinter 从入门到放弃(2)

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

Python tkinter 从入门到放弃(2)

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

文章目录:

一.pack()的使用

1.基本用法

2.方位(上下左右中)用法

(1).上下左右

(2).居中放置

3.内边距和外边距

(1).在y轴上

(2).在x轴上

4.真正的上下左右

 5.特殊方位

                                        欢迎补充

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

一.pack()的使用

注意:pack函数是一行一个控件(除fill),所以如果要让多个控件于一行上,请到Python tkinter 从入门到放弃(3)或(4)

1.基本用法
import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口

label_1 = tk.Label(root,text = "我是一个标签")   #Label用法见Python tkinter 从入门到放弃(3)
label_1.pack()

root.mainloop()    #消息循环

pack最基本的用法就是 控件变量.pack(),作用就是居中放置

pack()只在左右居中,没有上下居中 (但pack(expand = "yes")和其他参数可以 [dogs])

运行程序:

2.方位(上下左右中)用法

(1).上下左右

pack()   →   pack(tk.N/W/E/S)

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

bearing = ["上","左","右","下"]
bearing_p = [tk.N,tk.W,tk.E,tk.S]
           #(N:上,W:左,E:右,S:下)

for i in range(4):
         label_1 = tk.Label(root,text = f"我是一个{bearing[i]}标签")
         label_1.pack(anchor = bearing_p[i])

root.mainloop()    #消息循环

因为Label控件里是汉字,所以默认为16像素为一行,如图:

 效果:左标签和右标签可以随窗口的大小而改动,但上标签和下标签不行

(2).居中放置

pack() → pack(expand = "yes")

                           可替换为   ↓

                                   “no”或1或0

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

bearing = ["上","左","右","下"]
bearing_p = [tk.N,tk.W,tk.E,tk.S]
          #(N:上,W:左,E:右,S:下)
for i in range(4):
         label_1 = tk.Label(root,text = f"我是一个向{bearing[i]}的标签")
         label_1.pack(anchor = bearing_p[i])
         if i == 1:    #在第二个标签后插入
                  label_1 = tk.Label(root,text = f"我是一个居中的标签")
                  label_1.pack(expand = "yes")    #可改为"no",1,0
                  #label_1.pack(expand = "no")

root.mainloop()    #消息循环

expand是居中函数

当expand = "yes"或1时,如视频所示:

当expand = "no"或0时,如视频所示:

注意:expand不是整个窗口的中心,而是控件剩余位置的中心

详细参考:小白学tkinter(pack属性fill和expand)_代码_终结者的博客-CSDN博客_pack(fill)

3.内边距和外边距

(1).在y轴上
import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口

label_1 = tk.Label(root,text = f"我是一个标签",bg = "green",fg = "green")
#将label变成一个绿色的矩形
#label_1.pack()
label_1.pack(ipady = 10)    #label在y轴上内边距为10像素
label_1.pack(pady = 50)    #label在y轴上外边距为50像素

root.mainloop()    #消息循环

pack()的大小如图所示:

 pack(ipady = 10)#有内边距时大小如图所示:

 内边距10像素就是把控件外延10像素

pack(pady = 50)#有外边距时大小如图所示:

 外边距50像素就是把控件外延50像素但不填充控件内容

(2).在x轴上
import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口

label_1 = tk.Label(root,text = f"我是一个标签",bg = "green",fg = "green")
#将label变成一个绿色的矩形
#label_1.pack()
label_1.pack(ipadx = 10)    #label在x轴上内边距为10像素
label_1.pack(padx = 50)    #label在x轴上外边距为50像素

root.mainloop()    #消息循环

pack()的大小如图所示:

 pack(ipadx = 10)#有内边距时大小如图所示:

 内边距10像素就是把控件外延10像素

pack(padx = 50)#有外边距时大小如图所示:

 外边距50像素就是把控件外延50像素但不填充控件内

4.真正的上下左右

pack()  → pack(side = "top"或“bottom”或"left"或“right”)

                                    #上           下          左        右

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

where = ["上","上","下","下","左","左","右","右"]
where_pack = ["top","top","bottom","bottom","left","left","right","right"]
for i in range(len(where)):
         label_1 = tk.Label(root,text = f"{where[i]}{i}")
         label_1.pack(side = where_pack[i])    #方位

root.mainloop()    #消息循环

运行程序:

注意:tk.S不能随窗口大小而改变,但side可以,如视频所示,如果继续编写的画,路径如图所示:

 5.特殊方位

pack()   →   pack(anchor = “方位(英文)”)

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

where = ["北","东北","东","东南","南","西南","西","西北"]    #方位(text)
pack_where = ["n","ne","e","se","s","sw","w","nw"]    #方位(pack())
for i in range(len(where)):
         label_1 = tk.Label(root,text = f"{where[i]}")
         label_1.pack(anchor = pack_where[i])    #放置

root.mainloop()    #消息循环

运行结果:

因为读者可能看不出来,所以改动了一下:

 其中,汉字底下的英文就是其对应的首字母(很重要!)

可惜的是它和tk.S一样,上下不能随窗口变化而变化

                                        欢迎补充

附tkinter pack函数源码(注释已翻译):
class Pack:
    """几何管理器 pack.base 类在每个小部件中使用 pack _ * 方法."""

    def pack_configure(self, cnf={}, **kw):
        """Pack a widget in the parent widget. Use as options:
        after=widget - pack it after you have packed widget
        anchor=NSEW (or subset) - position widget according to
                                  given direction
        before=widget - pack it before you will pack widget
        expand=bool - expand widget if parent size grows
        fill=NONE or X or Y or BOTH - fill widget if widget grows
        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
        side=TOP or BOTTOM or LEFT or RIGHT -  where to add this widget.
        """

        """在父窗口小部件中打包一个窗口小部件。使用选项: after = widget-pack it after you             
           have pack widget 锚 = nsew (或子集)-position widget after = widget 根据给定的方 
           向定位 widget before = widget-pack it before you will pack widget exped = 
           bool-expget widget if father size growth fill = none or x or y or both-fill                       
           widget if widget growth in = master-use master to include this widget in _ = 
           master-see‘ in’options description ipadx = amount-add Internal packing in x  
           方向 ipady = amount-add Internal paddx = amount-add Internal paddin x way 
           paddy = amount-add paddin y way side = top or bottom or left or right-where to 
           add this widget"""
        self.tk.call(
              ('pack', 'configure', self._w)
              + self._options(cnf, kw))

    pack = configure = config = pack_configure

    def pack_forget(self):
        """取消映射此小部件,并且不要将其用于包装订单"""
        self.tk.call('pack', 'forget', self._w)

    forget = pack_forget

    def pack_info(self):
        """返回有关此小部件的打包选项的信息"""
        d = _splitdict(self.tk, self.tk.call('pack', 'info', self._w))
        if 'in' in d:
            d['in'] = self.nametowidget(d['in'])
        return d

    info = pack_info
    propagate = pack_propagate = Misc.pack_propagate
    slaves = pack_slaves = Misc.pack_slaves

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

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

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