TkInter单选按钮将每个选项置于彼此之上

时间:2018-07-20 18:14:28

标签: python tkinter

我正在尝试实现4选项单选按钮,但是,每当我尝试将单选按钮放置在窗口内时,每个选项都将放在另一个顶部,而不是一个在另一个下面:

MODES = [
        ("First ", "1"),
        ("Second", "L"),
        ("Third ", "RGB"),
        ("Fourth", "CMYK")
]

answer = StringVar()
answer.set("L")

for text, mode in MODES:
    RB = Radiobutton(root, text=text, variable = answer, value = mode, font  = ("Segoe UI", 14), )
RB.place(x=20, y=90)

但是,当我包装单选按钮而不是放置单选按钮时,似乎将选项一个放置在另一个下方,那么有没有一种使用放置功能的方法,并且选项仍然显示在另一个下方?提前致谢。这是供参考的屏幕截图:

As you can see, there's only one option being displayed, and all the others are underneath.

1 个答案:

答案 0 :(得分:0)

此代码片段将在垂直堆栈中显示单选按钮。我使用了额外的tk.Frame从窗口到控件添加了填充(只需为所有控件指定一次)。

import tkinter as tk

MODES = [
        ("First ", "1"),
        ("Second", "L"),
        ("Third ", "RGB"),
        ("Fourth", "CMYK")
]

root = tk.Tk()

answer = tk.StringVar()
answer.set("L")

frame = tk.Frame(root)
frame.pack(padx=10, pady=10, fill=tk.BOTH)

tk.Label(frame, text="Choose your language:").pack()

for text, mode in MODES:
    RB = tk.Radiobutton(frame, text=text, variable = answer, value = mode)
    RB.pack(anchor="w")

root.mainloop()