我需要使用 Frame 小部件来执行此操作;我知道我可以在不使用框架的情况下做到这一点,只需使用一个大网格来包含所有内容(画布的列跨度等),但如果可能的话,我想将东西放入框架中。
有关我的软件版本的快速信息:
=> OS: macOS Catalina 10.15.7
=> IDE: PyCharm CE
=> Python version: 3.12.5
=> tkinter Tcl version: 8.6 (from importing tkinter and using: print(tkinter.TclVersion))
我正在尝试实现以下目标:
我尝试了以下方法,但没有成功:
就像我上面说的,如果我去掉框架和网格所有东西,我可以很容易地做到这一点,如下所示:
那么考虑到我正在尝试实现的内容,我错过/做错了什么,导致我无法扩展网格的宽度,使其基本上填充整个窗口宽度?
我的代码如下。
from tkinter import *
# check to see if we're in macOS
import platform
inMac = False # global flag to check whether we're in macOS
if platform.system() == "Darwin":
inMac = True
print("Using macOS ...")
from tkmacosx import Button
window = Tk()
frame = Frame(window, width=500) # [NO HELP]: Setting frame attribute: width=500.
frame.pack()
#frame.grid(row=0,column=0) # [NO HELP]: Trying to put the frame and canvas in grid.
frame.pack_propagate(0) # [NO HELP]: Using Frame.pack_propagate(0) to stop child widgets
# from affecting the dimensions of the frame widget.
button = Button(frame, text="Button")
button.grid(row=0, column=0, sticky=W) # [NO HELP]: Setting "sticky" attribute.
#button.pack(side=LEFT, fill="x", expand=True) # [NO HELP]: Setting "expand" attribute with Button.pack().
label_1 = Label(frame, text="Whatever #1")
label_1.grid(row=0, column=1)
#label_1.pack(side=LEFT, fill="x", expand=True) # [NO HELP]: Setting "expand" attribute with Label.pack().
label_2 = Label(frame, text="Whatever #2")
label_2.grid(row=0, column=2, sticky=E) # [NO HELP]: Setting "sticky" attribute.
#label_2.pack(side=LEFT, fill="x", expand=True) # [NO HELP]: Setting "expand" attribute with Button.pack().
frame.columnconfigure(0, weight=1) # [NO HELP]: Setting equal weights for each column
frame.columnconfigure(1, weight=1)
frame.columnconfigure(2, weight=1)
canvas = Canvas(window, width=500, height=500)
#canvas.grid(row=1, column=0) # [NO HELP]: (As above) Trying to put the frame and canvas in grid.
canvas.pack()
# [NO HELP]: Update window before we display it
#window.update()
window.mainloop()
谢谢你。
为了实现你想要的,你需要:
fill='x'
添加到frame.pack(...)
,使框架水平扩展至与画布宽度相同uniform=<something>
上添加 .grid(...)
以使三列具有相同的宽度。 具有相同 uniform
值的列将具有相同的宽度。以下是修改后的代码:
from tkinter import *
import platform
inMac = False
if platform.system() == "Darwin":
inMac = True
print("Using macOS ...")
from tkmacosx import Button
window = Tk()
frame = Frame(window)
frame.pack(fill='x') # added fill='x'
button = Button(frame, text="Button")
button.grid(row=0, column=0, sticky=EW)
label_1 = Label(frame, text="Whatever #1", bg='yellow')
label_1.grid(row=0, column=1, sticky=EW)
label_2 = Label(frame, text="Whatever #2", bg='cyan')
label_2.grid(row=0, column=2, sticky=EW)
frame.columnconfigure(0, weight=1, uniform='a') # added uniform='a'
frame.columnconfigure(1, weight=1, uniform='a')
frame.columnconfigure(2, weight=1, uniform='a')
canvas = Canvas(window, width=500, height=500, bg='pink')
canvas.pack(fill='both', expand=1)
window.mainloop()
请注意,我已在这些按钮和标签上将
sticky=EW
添加到 .grid(...)
,以便这些小部件将扩展以填充分配的空间。 还可以在这些小部件上设置背景颜色以显示最终效果。
输出:
请注意,我的平台是Windows,但我认为在MacOS上结果会是相同的。