我的问题是:输出应该显示 2 个侧边栏(第 0 列和第 2 列),其中包含内容标签和条目以及一个中央窗口(第 1 列),比中间的其他侧边栏大得多。但中间的列总是出现在右侧,并且作为一个非常小的框架。请帮忙。我的代码和图片:import tkinter as tk, tkinter.ttk as ttkroot = tk.Tk()root.title("THE FRIDGER")root.grid_columnconfigure(0, weight=1)root.grid_rowconfigure(2, weight=1)#prepared datadflt = dict(fg="white", bg="black")pads = dict(pady=4, padx=4)#header frameheader = tk.Frame(root, bg="black")header.grid(row=0, column=0, columnspan=3, sticky="nsew")for i in range(2): header.grid_columnconfigure(i, weight=1)#header labelstk.Label(header, text="Fridge", **dflt).grid(column=0, row=0, **pads)tk.Label(header, text="Recipes", **dflt).grid(column=1, row=0, **pads)#separators = ttk.Style()s.configure('custom.TSeparator', background='blue')ttk.Separator(root, style='custom.TSeparator').grid(row=1, column=0, columnspan=3, sticky="ew")#left side contentl_content = tk.Frame(root, bg="black")l_content.grid(row=2, column=0, sticky="nsew")tk.Label(l_content, text="Content:", **dflt).grid(column=0, row=0, sticky=tk.W)l_query = tk.Entry(l_content, width=36, relief=tk.FLAT, bg = "white", fg = "black")l_query.grid(column=0, row=1, sticky=tk.W)#right side contentr_content = tk.Frame(root, bg="black")r_content.grid(row=2, column=2, sticky="nsew")tk.Label(r_content, text="Content:", **dflt).grid(column=0, row=2, sticky=tk.W)r_query = tk.Entry(r_content, width=36, relief=tk.FLAT, bg = "white", fg = "black")r_query.grid(column=0, row=3, sticky=tk.W)#middle contentm_content = tk.Frame(root, bg="white")m_content.grid(row=2, column=1, sticky="nsew")tk.Label(m_content, text="This should appear in the middle", **dflt).grid(column=0, row=2, sticky=tk.W)m_content.grid_columnconfigure(1, weight = 1)root.mainloop()
2 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
左列如此大的原因是您给它的权重为 1。如果您希望中心列占据所有额外空间,您需要给它一个正权重,而不是给第 0 列权重。
root.grid_columnconfigure(1, weight=1)
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您设置weight=1
错误的列:
root.grid_columnconfigure(0, weight=1)
应该root.grid_columnconfigure(1, weight=1)
m_content.grid_columnconfigure(1, weight=1)
应该m_content.grid_columnconfigure(0, weight=1)
并sticky
从以下位置删除选项:
tk.Label(m_content, text="This should appear in the middle", **dflt).grid(column=0, row=2, sticky=tk.W)
添加回答
举报
0/150
提交
取消