如何将ttk中的复选框左对齐?
要将复选框对齐到左侧,可以使用anchor参数,并将其设置为“w”(表示西侧)。让我们举个例子,看看如何做。
步骤 –
- 导入tkinter库并创建tkinter框架的实例。
-
使用geometry方法设置框架的大小。
-
创建LabelFrame以将复选框分组。
-
接下来,在LabelFrame中创建复选框,并将它的anchor设置为West。 anchor=’w’ 。
-
同样,创建其他三个anchor设置为West的复选框。这将把所有复选框左对齐。
-
最后,运行应用程序窗口的mainloop。
示例
from tkinter import *
root = Tk()
root.geometry("700x350")
# 创建LabelFrame
frame = LabelFrame(root, text="选择科目", padx=20, pady=20)
frame.pack(pady=20, padx=10)
# 在框架内创建四个复选框
C1 = Checkbutton(frame, text="数学", width=200, anchor="w").pack()
C2 = Checkbutton(frame, text = "物理", width=200, anchor="w").pack()
C3 = Checkbutton(frame, text = "化学", width=200, anchor="w").pack()
C4 = Checkbutton(frame, text = "生物", width=200, anchor="w").pack()
root.mainloop()
输出
执行后,将产生以下输出 –