擅长:python、mysql、java
<p>您是否考虑过为此使用<code>.pack()</code>方法。这样可以更容易地达到预期效果:</p>
<pre><code>from tkinter import *
root = Tk()
top = Toplevel(root)
w = '400'
h = '100'
top.geometry('{}x{}'.format(w, h))
frame = Frame(top)
label = Label(frame, text="This is an error message")
button = Button(frame, text="Ok")
frame.pack(expand=True) #expand assigns additional space to the frame if the parent is expanded
label.pack()
button.pack()
root.mainloop()
</code></pre>
<hr/>
<p>经过一些研究,使用网格做这件事比预期的要容易得多,如下所示:</p>
<pre><code>from tkinter import *
root = Tk()
w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))
label = Label(root, text="text")
label.grid(column=0, row=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
root.mainloop()
</code></pre>
<p>如果我们分配<code>.rowconfigure()</code>和<code>.columnconfigure()</code>一个<code>weight</code>而不是<code>0</code>,那么指定的行和列将展开以填充窗口中给它的空间。</p>