Tkinter中的标题和按钮位置

时间:2017-12-20 19:29:45

标签: python-3.x tkinter tkinter-layout

很抱歉这个不太漂亮的代码,但这只是我遇到问题的一个简单示例。

我希望问题和按钮在彼此之下,只要按钮不在彼此旁边就可以正常工作:

from tkinter import *
root = Tk()
root.title('Test')
v = IntVar()
Label(root, text= "1st question", justify=LEFT).pack(padx=10)
Radiobutton(root, text="Button1",indicatoron = 0, width = 10, variable=v, value=0).pack()
Radiobutton(root, text="Button2",indicatoron = 0, width = 10, variable=v, value=1).pack()
Radiobutton(root, text="Button3",indicatoron = 0, width = 10, variable=v, value=2).pack()
Radiobutton(root, text="Button4",indicatoron = 0, width = 10, variable=v, value=3).pack()
Radiobutton(root, text="Button5",indicatoron = 0, width = 10, variable=v, value=4).pack()

b = IntVar()
Label(root, text= "2nd question", justify=LEFT).pack(padx=10)
Radiobutton(root, text="Button1",indicatoron = 0, width = 10, variable=b, value=0).pack()
Radiobutton(root, text="Button2",indicatoron = 0, width = 10, variable=b, value=1).pack()
Radiobutton(root, text="Button3",indicatoron = 0, width = 10, variable=b, value=2).pack()
Radiobutton(root, text="Button4",indicatoron = 0, width = 10, variable=b, value=3).pack()
Radiobutton(root, text="Button5",indicatoron = 0, width = 10, variable=b, value=4).pack()
root.mainloop()

我确定有一些我错过的简单内容,你可以查看我目前的代码:

from tkinter import *
root = Tk()
root.title('Test')
v = IntVar()
Label(root, text= "1st question", justify=LEFT).pack(side=TOP, padx=10)
Radiobutton(root, text="Button1",indicatoron = 0, width = 10, variable=v, value=0).pack(side=LEFT)
Radiobutton(root, text="Button2",indicatoron = 0, width = 10, variable=v, value=1).pack(side=LEFT)
Radiobutton(root, text="Button3",indicatoron = 0, width = 10, variable=v, value=2).pack(side=LEFT)
Radiobutton(root, text="Button4",indicatoron = 0, width = 10, variable=v, value=3).pack(side=LEFT)
Radiobutton(root, text="Button5",indicatoron = 0, width = 10, variable=v, value=4).pack(side=LEFT)

b = IntVar()
Label(root, text= "2nd question", justify=LEFT).pack(side=TOP, padx=10)
Radiobutton(root, text="Button1",indicatoron = 0, width = 10, variable=b, value=0).pack(side=LEFT)
Radiobutton(root, text="Button2",indicatoron = 0, width = 10, variable=b, value=1).pack(side=LEFT)
Radiobutton(root, text="Button3",indicatoron = 0, width = 10, variable=b, value=2).pack(side=LEFT)
Radiobutton(root, text="Button4",indicatoron = 0, width = 10, variable=b, value=3).pack(side=LEFT)
Radiobutton(root, text="Button5",indicatoron = 0, width = 10, variable=b, value=4).pack(side=LEFT)
root.mainloop()

1 个答案:

答案 0 :(得分:2)

将按钮放在框架中,然后正常打包框架。

Label(root, text= "1st question", justify=LEFT).pack(padx=10)
buttonframe = Frame(root)
Radiobutton(buttonframe, text="Button1",indicatoron = 0, width = 10, variable=v, value=0).pack(side=LEFT)
Radiobutton(buttonframe, text="Button2",indicatoron = 0, width = 10, variable=v, value=1).pack(side=LEFT)
Radiobutton(buttonframe, text="Button3",indicatoron = 0, width = 10, variable=v, value=2).pack(side=LEFT)
Radiobutton(buttonframe, text="Button4",indicatoron = 0, width = 10, variable=v, value=3).pack(side=LEFT)
Radiobutton(buttonframe, text="Button5",indicatoron = 0, width = 10, variable=v, value=4).pack(side=LEFT)
buttonframe.pack()