python - 如何将多个 tkinter 小部件放入一个 .grid 中

标签 python tkinter

我正在尝试使用 python 制作日期选择器。我正在使用旋转盒,但是我想知道是否可以将所有 5 个小部件放入一个网格空间中,所以看起来所有 5 个小部件实际上都是一个小部件。希望下面的代码能够更好地阐明问题。

import tkinter as tk

root=tk.Tk()
Day=tk.IntVar()
Month=tk.IntVar()
Year=tk.IntVar()
Label1=tk.Label(root,text="Label Label Label Expanding Row")
Label1.grid(row=1,column=1)

DayEntry=tk.Spinbox(root,textvariable=Day,bg="white",from_=0, to_=31,width=2)
DayEntry.grid(row=2,column=1)
MonthEntry=tk.Spinbox(root,textvariable=Month,bg="white",from_=0, to_=12,width=2)
MonthEntry.grid(row=2,column=3)
YearEntry=tk.Spinbox(root,textvariable=Year,bg="white",from_=2000, to_=20019,width=4)
YearEntry.grid(row=2,column=5)
Divider1=tk.Label(root,text="/")
Divider1.grid(row=2,column=2)
Divider2=tk.Label(root,text="/")
Divider2.grid(row=2,column=4)


root.mainloop()

最佳答案

解决方案是将所有小部件放在一个框架中。

datepicker = tk.Frame(root)
datepicker.grid(row=2, column=0)

DayEntry=tk.Spinbox(datepicker,textvariable=Day,bg="white",from_=0, to_=31,width=2)
MonthEntry=tk.Spinbox(datepicker,textvariable=Month,bg="white",from_=0, to_=12,width=2)
YearEntry=tk.Spinbox(datepicker,textvariable=Year,bg="white",from_=2000, to_=20019,width=4)
Divider1=tk.Label(datepicker,text="/")
Divider2=tk.Label(datepicker,text="/")

DayEntry.grid(row=0,column=1)
Divider1.grid(row=0,column=2)
MonthEntry.grid(row=0,column=3)
Divider2.grid(row=0,column=4)
YearEntry.grid(row=0,column=5)

关于python - 如何将多个 tkinter 小部件放入一个 .grid 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53324268/

相关文章:

使用 Tkinter 的带有 GUI 的 Python 扫雷游戏如何正确显示在按钮上?

python - 使用按钮从 Tkinter 条目搜索框中获取文本

python - pandas 中的自定义 bin 和 sum ?

python - 有时 pip install 很慢

python - 为什么我的测试 sqlite 数据库的 'filling' 占用了这么多 RAM 并且没有保存任何内容?

python - 如何使用 Pillow 显示图像?

python - 在 Python 中有效确定幂集成员之间的子集

python - "invalid default value"带有 lxml 和 ATTLIST 标签

Python 文件 - 每次打开时写入新行

python - 为什么我使用pystray时无法关闭?