gpt4 book ai didi

python - 更改 tkinter 窗口中按钮的位置

转载 作者:行者123 更新时间:2023-12-01 02:41:16 25 4
gpt4 key购买 nike

我有一个复选框窗口,上面还有一个完成按钮(提交)。该按钮当前位于窗口顶部,我如何将其放在底部。我尝试过诸如 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()

最佳答案

done_btn.pack()移至c6.pack()

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

话虽这么说,让我解释一下原因。

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

正如 Bryan 在评论中指出的那样,如果使用正确,pack() 会非常有用。

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

考虑将所有内容更改为 grid(),而不是使用 pack(),这样您就可以更轻松地定义事物的去向。

使用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()

更新:

只是对旧答案的快速更新。为了更加干燥和动态,我们可以使用一些列表和“for 循环”来减少执行相同任务所需的代码。

示例:

import tkinter as tk

check = tk.Tk()
check.title("Interests")
var_list = []
check_list = []
text_list = ['Horror', 'Action', 'Documentary', 'Science fiction', 'Comedy', 'Drama']

for ndex, v in enumerate(text_list):
var_list.append(tk.IntVar())
check_list.append(tk.Checkbutton(check, text=v, variable=var_list[-1], onvalue=1, offvalue=0, height=1, width=20))
check_list[-1].grid(row=ndex, column=0)

tk.Button(check, text='Done', command=check.destroy).grid(row=len(text_list)+1, column=0)
check.mainloop()

当我们更严格地遵循 PEP-8 标准并以更 DRY 的方式编写时,这是 26 行的差异。

结果:

enter image description here

关于python - 更改 tkinter 窗口中按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com