首页 > 解决方案 > tkinter:如何使这些框适合左侧并成直线

问题描述

如您所见,我的盒子不在一条直线上。我想让它们在直线上,更靠左。我的代码:

has_lower_box = tk.Checkbutton(root, text="lower chars", variable=has_lower_value)
has_lower_box.grid(row=0, column=1)
has_upper_box = tk.Checkbutton(root, text="upper chars", variable=has_upper_value)
has_upper_box.grid(row=1, column=1)
has_upper_box = tk.Checkbutton(root, text="special chars", variable=has_special_value)
has_upper_box.grid(row=2, column=1)

复选框

标签: pythonuser-interfacetkinteruser-experience

解决方案


sticky= 以下是如何使用Tkinter Grid Geometry Manager选项将它们排列起来。我将它设置sticky=W为 West,这当然是指左侧。

import tkinter as tk
from tkinter.constants import *

root = tk.Tk()

has_lower_value = tk.BooleanVar()
has_upper_value = tk.BooleanVar()
has_special_value = tk.BooleanVar()

has_lower_box = tk.Checkbutton(root, text="lower chars", variable=has_lower_value)
has_lower_box.grid(row=0, column=1, sticky=W)
has_upper_box = tk.Checkbutton(root, text="upper chars", variable=has_upper_value)
has_upper_box.grid(row=1, column=1, sticky=W)
has_upper_box = tk.Checkbutton(root, text="special chars", variable=has_special_value)
has_upper_box.grid(row=2, column=1, sticky=W)

root.mainloop()

这是显示结果的放大屏幕截图:

放大截图


推荐阅读