我需要帮助在Tkinter Radiobutton
正如您所看到的,它并不是完全一致的。我怎样才能获得"目标"要垂直对齐的文字?我是这样做的:
pairs = [None for x in range(10)]
for i in range(len(startList)):
pairs[i] = (''.join(["Start: (", str(startList[i].X), ",", str(startList[i]), ")", '{:>20}'.format(''.join(["Goal: (", str(goalList[i].X), ",", str(goalList[i].Y), ")"]))]), i)
radioRow = Frame(self)
radioRow.pack(fill=Y)
v = IntVar()
v.set(0)
for text, mode in pairs:
rdButton = Radiobutton(radioRow, text=text, variable=v, value=mode)
rdButton.pack(anchor=W)
答案 0 :(得分:2)
将文本分成两个小部件:radiobutton和label。然后制作radiobuttons的父级并标记一个帧,并使用grid
将它们排列成两列十行矩阵。
这是一个粗略的例子:
import Tkinter as tk
data = (
((111,2), (14,90)),
((46, 1), (16, 111)),
((94, 1), (16, 111)),
)
root = tk.Tk()
choices = tk.Frame(root, borderwidth=2, relief="groove")
choices.pack(side="top", fill="both", expand=True, padx=10, pady=10)
v = tk.StringVar()
for row, (start, goal) in enumerate(data):
button = tk.Radiobutton(choices, text="Start (%s,%s)" % start, value=start, variable=v)
label = tk.Label(choices, text="Goal: (%s, %s)" % goal)
button.grid(row=row, column=0, sticky="w")
label.grid(row=row, column=1, sticky="w")
# give the invisible row below the last row a weight, so any
# extra space is given to it
choices.grid_rowconfigure(row+1, weight=1)
root.mainloop()
答案 1 :(得分:1)
您必须对齐Start
,而不是Goal
- {:<10}
- 因此它将始终使用10个字符。然后Goal
将在同一个地方开始。但它理想情况下只适用于等宽字体
data = [
(111, 2, 14, 90),
(46, 1, 16, 111),
(94, 1, 38, 1),
]
for a, b, c, d in data:
start = "({},{})".format(a, b)
goal = "({},{})".format(c, d)
print("Start: {:<10} Goal: {}".format(start, goal))
结果:
Start: (111,2) Goal: (14,90)
Start: (46,1) Goal: (16,111)
Start: (94,1) Goal: (38,1)
BTW:您还可以使用grid()
创建两列 - 一列Radiobutton
和Start
,第二列Label
和{{ 1}}