python中的Tkinter grid()对齐问题

Bry*_*yce 0 python grid tkinter rows multiple-columns

我第一次在 python 中工作,基本上我试图让这些标签在正确的列和正确的行中对齐,但由于某种原因它不会向下移动行,列也不正确。任何帮助将非常感激!

代码:

    from tkinter import *
    import sys

    #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();
    #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,columnspan=4, 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, columnspan=4, sticky = W);
Run Code Online (Sandbox Code Playgroud)

现在我会附上一张图片给你们看看它是如何显示的......这是不正确的:-(

 网格问题。

ash*_*njv 5

如果一行或一列不包含任何内容,则它的大小为 1 像素,这是非常小的。您在输出中看到的实际上是相距 20 行的两个文本。添加小部件,您将看到结果。

您还可以使用grid_rowconfigure,grid_columnconfiguresticky属性来指定网格中的小部件如何拉伸。这样您就可以将小部件放置在正确的屏幕位置。

有关如何使用网格属性的更多详细信息,请查看我的答案:Tkinter。根子帧不显示

为了更好地理解您的网格,我在您的代码中添加了另一个小部件:

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()
Run Code Online (Sandbox Code Playgroud)

尝试这个。

PS:你不需要;在每一行python之后