Tkinter教程之pack grid place 放置位置

51changxuePython评论903 views阅读模式

Tkinter教程之pack grid place 放置位置
1.pack
首先我们先看看我们常用的pack(), 他会按照上下左右的方式排列.

tk.Label(window, text='top').pack(side='top')
tk.Label(window, text='bottom').pack(side='bottom')
tk.Label(window, text='left').pack(side='left')
tk.Label(window, text='right').pack(side='right')

2.grid

接下里我们在看看grid(), grid 是方格, 所以所有的内容会被放在这些规律的方格中.

for i in range(4):
    for j in range(3):
        tk.Label(window, text=1).grid(row=i, column=j, padx=10, pady=10)

以上的代码就是创建一个四行三列的表格,其实grid就是用表格的形式定位的。这里的参数row为行,colum为列,padx就是单元格左右间距,pady就是单元格上下间距。

3.place

再接下来就是place(), 这个比较容易理解,就是给精确的坐标来定位,如此处给的(20,10),就是将这个部件放在坐标为(x,y)的这个位置 后面的参数anchor=nw就是前面所讲的锚定点是西北角。

tk.Label(window, text=1).place(x=20, y=10, anchor='nw')

代码:

import tkinter as tk

from tkinter import *

window = tk.Tk()
window.title('pack和place')
window.geometry('200x200')
tk.Label(window, text='top').pack(side='top')
tk.Label(window, text='bottom').pack(side='bottom')
tk.Label(window, text='left').pack(side='left')
tk.Label(window, text='right').pack(side='right')
tk.Label(window, text='x80:y80').place(x=80, y=80, anchor='nw')
def anotherwindow():#grid和place不能在同一个类或窗口里使用,调用函数生成另外一个窗口
 root= tk.Tk()
 root.geometry('200x200')
 root.title('grid')
 for i in range(4):
 for j in range(3):
 tk.Label(root, text=1).grid(row=i, column=j, padx=10, pady=10)


 root.mainloop()
b = Button(window,
 text="Button",
 command=anotherwindow,#按钮动作执行动作
 width=15,
 height=2,
 bg='red',#背景色
 fg='green',#前景色
 anchor='s',#文本在控件上的位置
 bd='2',#边框

 ).place(x=50,y=100)
window.mainloop()


运行结果:

Tkinter教程之pack grid place 放置位置

继续阅读
51changxue
  • 本文由 发表于 2018年4月10日 01:11:12
  • 转载请务必保留本文链接:https://51changxue.com/458.html
pycharm永久破解激活教程 Python

pycharm永久破解激活教程

PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、...
Tkinter控件快速引用总结 Python

Tkinter控件快速引用总结

  文件名称: Tkinter控件快速引用总结代码.rar 文件大小: 4k 更新日期: 2018-7-29 文件版本: V1.0 免责声明: 本站大部分下载资源收集于网络,只做学习和交流使...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定