Tkinter语言 - 网格定位对某些Widjet完全没有反应



我有这段代码(一些代码被剪掉了(:

root = tk.Tk()
root.title("PyCipher")
root.iconbitmap('D:\PyCipher\media\icon.ico')
root.geometry('510x325')
root.resizable(width=False, height=False)
root.columnconfigure(10, weight=1)
root.rowconfigure(20, weight=1)
rootframe = tk.Frame(root)
rootframe.grid(row=10, column=5)
headertxt = tk.Label(root, text='PyCipher', font=('Helvecta', 20, 'bold'), anchor='c')
headertxt.grid(row=3, column=5)
entrybox = tk.Entry(root, width=60)
entrybox.grid(row=7, column=5)
entrylabel = tk.Label(root, text='Enter Text to convert')
entrylabel.grid(row=9, column=5)
output = tkst.ScrolledText(master=rootframe,wrap=tk.WORD, width=50, height=10)
output.grid(row=10, column=8)
outputlabel = tk.Label(root, text='Output text')
outputlabel.grid(row=15, column=5)
encodebtn = tk.Button(root, text='Encode', fg='green', command=encode, width=5)
encodebtn.grid(row=7, column=4)
decodebtn = tk.Button(root, text='Decode', fg='orange', command=decode, width=5)
decodebtn.grid(row=7, column=6)
copybtn = tk.Button(root, text='Copy', fg='blue', command=copy, width=5)
copybtn.grid(row=17, column=5)
clrbtn = tk.Button(root, text='Clear', fg='red', command=clear, width=5)
clrbtn.grid(row=18, column=5)

root.mainloop()

问题出在按钮上。当我尝试将列值更改为 2-4 或 6-9 时,元素对位置更改完全没有响应。但是当我将其设置为第 5 列时,它通过输入另一个 widjet 完全破坏了 UI。为什么我不能移动其他列中的任何元素,而第 5 列却将元素完全移动到应有的区域?填充最终会填充整个 UI,即使元素远离其他元素,它仍然存在相同的问题。

网格布局管理器的规则是,它将布局压缩到行或列中任何小部件的最大大小。因此,列 1、2 和 3 的宽度为零,因为它们不包含小部件。如果将第 4 列中的小部件移动到这些列之一,则不会对布局的可见状态产生影响。第 4 列现在的宽度为零,其空间将被您将小部件移动到的列占用。至于你的说法"填充最终填充整个UI",我不明白你在这里的意思,在那里帮不了你。

在这里查看grid_rowconfiguregrid_columnconfigure

我似乎记得不久前遇到过类似的问题。您需要给每一列/每一行一个权重。

最新更新