繁体   English   中英

python中的Tkinter grid()对齐问题

[英]Tkinter grid() alignment issue in python

我第一次使用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);

现在,我将为您附加一个图像,以查看其显示方式...这是不正确的:-(

网格问题。

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

您还可以使用grid_rowconfiguregrid_columnconfiguresticky属性来指定网格grid_columnconfigure部件的拉伸方式。 这样,您可以将小部件放置在正确的屏幕位置。

请在这里查看我的答案,以获取有关如何使用网格属性的更多详细信息: 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()

尝试这个。

PS:您不需要; 每行python之后

空行的高度为零,空列的宽度为零。 网格的行为与您的代码设计一致,因为第1-19行中没有小部件,而第0-19列中没有小部件。 在其他行和列中放置一些内容,标签将移动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM