在tkinter窗口中更改按钮的位置

时间:2017-08-15 20:36:10

标签: python python-3.x tkinter tk

我有一个复选框窗口,上面还有一个完成按钮(提交)。按钮当前位于窗口的顶部,如何在底部显示它。 我尝试了done_btn.grid(row = 10)之类的东西,但似乎没有用。

我的代码:

from tkinter import *

check = Tk()
check.title("Interests")
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
CheckVar6 = IntVar()

C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)
C6 = Checkbutton(check, text = "Drama", variable = CheckVar6, 
                 onvalue = 1, offvalue = 0, height=1, 
                 width = 20)

done_btn = Button(check, text = "Done", command = lambda: check.destroy()) 
done_btn.pack()

C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
C6.pack()
check.mainloop()

1 个答案:

答案 0 :(得分:2)

done_btn.pack()移到c6.pack()

之后
C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
C6.pack()
done_btn.pack()

话虽如此,让我解释原因。

当您使用pack()而没有定义将小部件放置在tkinter上的side时,几何管理器将设置默认的.pack(side = "top")

正如布莱恩在评论pack()中指出的那样,如果使用得当,它会非常有用。

我更多地使用grid()工作,因此我更喜欢使用grid()因为我可以快速准确地定义布局。

不要使用pack(),而是考虑将所有内容更改为grid(),以便您可以更轻松地定义内容。

使用grid()

查看以下示例

您将看到无论我们设置网格的顺序,只要我们使用所需的行和列,小部件就会放置在我们想要的位置:

from tkinter import *

check = Tk()
check.title("Interests")
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()
CheckVar6 = IntVar()

C1 = Checkbutton(check, text = "Horror", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C2 = Checkbutton(check, text = "Action", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C3 = Checkbutton(check, text = "Documentary", variable = CheckVar3, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C4 = Checkbutton(check, text = "Science fiction", variable = CheckVar4, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C5 = Checkbutton(check, text = "Comedy", variable = CheckVar5, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
C6 = Checkbutton(check, text = "Drama", variable = CheckVar6, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)

done_btn = Button(check, text = "Done", command = lambda: check.destroy()) 
done_btn.grid(row = 6, column = 0)

C1.grid(row = 0, column = 0)
C2.grid(row = 1, column = 0)
C3.grid(row = 2, column = 0)
C4.grid(row = 3, column = 0)
C5.grid(row = 4, column = 0)
C6.grid(row = 5, column = 0)

check.mainloop()

结果:

enter image description here