我正在尝试填充滚动条以与文本框匹配,我知道如果您使用 fill 属性,您可以使用 pack() 来完成此操作,我该如何使用 grid() 来做到这一点?我试图找到一个相当于 fill 的属性(如果有的话)。
def bannergrabber():
top = Toplevel()
top.resizable(0, 0)
top.title("Website Banner Grabber")
top.configure(background="black")
labelframe = LabelFrame(top)
labelframe.config(bg="black")
labelframe.grid(row=1, column=0, padx=10)
labelframe2 = LabelFrame(top)
labelframe2.config(bg="black")
labelframe2.grid(row=0, column=0, pady=5, padx=10)
text = Text(labelframe2)
text1 = Text(top)
label2 = Label(labelframe2, text="Website:")
label2.config(bg="black")
label2.grid(row=0, pady=5, sticky=W)
text.config(width=25, height=1, bg="black")
text.grid(row=0, column=1)
def bannergrabbercode():
try:
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((text.get("1.0", 'end-1c'), 80))
sck.send(b"HEAD / HTTP/1.0\r\n\r\n")
data = sck.recv(1024)
sck.close()
text1.insert(END, data)
except socket.error:
text1.insert(END, "host is not reachable")
def clear():
text.delete("1.0", END)
b = Button(labelframe, text="Enter", bg="black", command=bannergrabbercode)
b.grid(row=1, column=0, sticky=W)
c = Button(labelframe, text="Clear", bg="black", command=clear)
c.grid(row=1, column=1, pady=5, padx=10, stick=W)
scrollbar = Scrollbar(top)
scrollbar.grid(row=2, column=1, sticky=W)
text1.config(width=40, height=10, bg="black")
text1.grid(row=2, pady=10, sticky=W)
scrollbar.config(bg="black", command=text1.yview)
text1.config(yscrollcommand=scrollbar.set)
top.mainloop()
我花了一段时间来构建这个布局,grid() 有时可能会令人困惑,但我不想回到使用 pack() 因为它是如此有限。
最佳答案
Does grid() has a fill attribute like pack() in tkinter?
是的,它是 sticky
属性,它告诉小部件“粘”到为其提供的空间的一侧或多侧。
对于pack
的fill="x"
,等效的grid
选项是sticky="ew"
(东-西)。对于 fill="y"
来说,等价的是 sticky="ns"
(北-南)。对于 fill="both"
来说,它是 sticky="nsew"
。 grid
比 pack 更灵活一点,因为您可以将小部件仅粘贴到一侧。
I don't want to go back to using pack() since is so limited.
pack
没有限制,只是有所不同。对于某些事情来说,它比grid
好得多,而对于另一些事情来说,它则更差。他们都有自己的位置。就我个人而言,我几乎从不创建不使用两者的 GUI。
关于python - grid() 是否有像 tkinter 中的 pack() 一样的 fill 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472105/