<p>如果一行或一列不包含任何内容,那么它的大小是1个像素,非常小。您在输出中看到的实际上是相隔20行的两个文本。添加小部件,您将看到您的结果。在</p>
<p>您还可以使用<code>grid_rowconfigure</code>、<code>grid_columnconfigure</code>和<code>sticky</code>属性来指定网格中小部件的拉伸方式。这样你就可以把你的小部件放在正确的屏幕位置。在</p>
<p>有关如何使用网格属性的详细信息,请参阅我的答案:<a href="https://stackoverflow.com/questions/25938465/tkinter-subframe-of-root-does-not-show/25939495#25939495">Tkinter. subframe of root does not show</a></p>
<p>为了更好地理解网格,我在代码中添加了另一个小部件:</p>
<pre><code>from tkinter import *
import sys
from tkinter.scrolledtext import ScrolledText
#Setup GUI
#Create main window
main = Tk()
#Create title of main window
main.title = "Waterizer 1.0"
#Create default size of main window
main.geometry("1024x768")
#Lets get a fram to stick the data in we plan on using
mainApp = Frame(main)
#Snap to grid
mainApp.grid(row=0, column=0, sticky='nsew')
#main grid stretching properties
main.grid_columnconfigure(0, weight=1)
main.grid_rowconfigure(0, weight=1)
#Font for main labels
labelFont = ('times', 20, 'bold')
#Label for the Power
powerLabel = Label(mainApp, text="Power Status")
powerLabel.config(fg="Red", bd="1")
powerLabel.config(font=labelFont)
powerLabel.grid( row=0,column=20, sticky = W)
#Label for Water
waterLabel = Label(mainApp, text="Water Status")
waterLabel.config(fg="Blue", bd="1")
waterLabel.config(font=labelFont)
waterLabel.grid(row=20, column=20, sticky = W)
#ScrollText to fill in between space
ScrollText = ScrolledText(mainApp)
ScrollText.grid(row=1, column=20,sticky = 'nsew')
#mainApp grid stretching properties
mainApp.grid_rowconfigure(1, weight=1)
mainApp.grid_columnconfigure(20, weight=1)
main.mainloop()
</code></pre>
<p>试试这个。在</p>
<p>PS:在python的每一行后面不需要<code>;</code></p>