【python】【tkinter】了解基础布局Pack

示例

import os,sys
import time
import tkinter as tk
from functools import partial

def test_command01(num0, num1):
    print(num0+num1)
    return

def test_command02(num0, num1):
    print(num0*num1)
    return


def create_tk_pack(top):
    # 创建两个容器
    frame0=tk.Frame(top)
    frame1=tk.Frame(top)

    # 创建按钮列表,避免定义过多变量,此时指令宽高,无实用
    butn_list=[]
    butn_list.append(tk.Button(frame0, text="测试按钮0+", height= 5, width=20,command=partial(test_command01, 0, 1)))
    butn_list.append(tk.Button(frame0, text="测试按钮0*", height= 5, width=20,command=partial(test_command02, 0, 1)))
    butn_list.append(tk.Button(frame1, text="测试按钮1+", height= 5, width=20, command=partial(test_command01, 2, 3)))
    butn_list.append(tk.Button(frame1, text="测试按钮1*", height= 5, width=20, command=partial(test_command02, 2, 3)))

    # 利用Pack,布局GUI
    frame1.pack(fill=tk.BOTH, expand=True, )
    frame0.pack(fill=tk.BOTH, expand=True, )
    butn_list[0].pack(fill=tk.BOTH, expand=True, side="left")
    butn_list[1].pack(fill=tk.BOTH, expand=True, side="left")
    butn_list[2].pack(fill=tk.BOTH, expand=True, side="top")
    butn_list[3].pack(fill=tk.BOTH, expand=True, side="top")
    return

if __name__ == "__main__":
    print(__file__)
    # 开始创建GUI运行程序
    top=tk.Tk()
    # 创建布局
    create_tk_pack(top)
    # 主线程运行
    top.mainloop()

    print("end")

创建子容器方便布局

    # 创建两个容器
    frame0=tk.Frame(top)
    frame1=tk.Frame(top)

响应函数传递入参

from functools import partial

def test_command02(num0, num1):
    print(num0*num1)
    return

    # 创建按钮列表,此时指令宽高,无实用
    butn_list=[]
    butn_list.append(tk.Button(frame0, text="测试按钮0+", height= 5, width=20,command=partial(test_command01, 0, 1)))

···

#  结束
未完待续
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值