You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter使用pack方法时如何将提交按钮对齐到日期下拉框下方

解决方案

你现在的问题是三个下拉框和提交按钮都在同一个父容器plane下,pack的side参数会让同层级控件在剩余空间内按方向排布,所以按钮和三个下拉框挤在了同一行。
你可以通过Frame容器分组的方式实现需求,把三个日期选择控件放到独立的Frame容器中占满第一行,提交按钮放在Frame的下一行即可。


修改后的完整代码

from tkinter import * # 若原有导入可忽略本句

# 原有变量定义无改动
dates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
         31]
month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUNE', 'JULY', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
year = [2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038,
        2039, 2040]

Date = StringVar(plane)
Date.set(dates[0])
Month = StringVar(plane)
Month.set(month[0])
Year = StringVar(plane)
Year.set(year[0])

# 新增:创建专门放置日期选择控件的Frame容器
date_frame = Frame(plane)
date_frame.pack(fill="x", expand=True) # 让Frame占满整行宽度

# 三个OptionMenu的父容器修改为date_frame,其余配置不变
date_option = OptionMenu(date_frame, Date, *dates)
month_option = OptionMenu(date_frame, Month, *month)
year_option = OptionMenu(date_frame, Year, *year)

date_option.config(font=("Arial", 18, "bold"))
month_option.config(font=("Arial", 18, "bold"))
year_option.config(font=("Arial", 18, "bold"))

date_option.pack(side="left", fill="x", expand="yes")
month_option.pack(side="left", fill="x", expand="yes")
year_option.pack(side="left", fill="x", expand="yes")

# 提交按钮父容器仍为plane,pack默认排在date_frame下方
submit = Button(plane, text="Submit", font=("Engrave", 15, "bold"), fg="black", bg="silver")
submit.pack(side="right", padx=30, pady=10) # pady参数可调整上下间距

核心逻辑说明

  • 新增的date_frame和提交按钮是plane下的同级控件,pack默认按从上到下的顺序排列同级控件,因此按钮会自动出现在日期选择栏的下方
  • 保留side="right"可以让按钮在第二行右对齐,你也可以根据需求去掉side参数实现居中/左对齐效果

内容的提问来源于stack exchange,提问作者JOHN ARTHUR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 00:24:04