python中的Tkinter grid()对齐问题 [英] Tkinter grid() alignment issue in python

查看:1883
本文介绍了python中的Tkinter grid()对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用python,基本上我正在尝试将它们添加到标签中以在正确的列和正确的行中对齐,但是由于某种原因,它不会向下移动行并且列不正确任何一个.任何帮助将不胜感激!

I have been working in python for the first time and basically I am trying to get these to labels to align in the correct columns and the correct row but for some reason it doesn't move down rows and the columns are not correct either. Any help would be much appreciated!

代码:

    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);

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

Now I will attach an image for you guys to see how it displays... which is not correct :-(

推荐答案

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

If a row or column does not contain anything, it is then of size 1 pixel, which is very small. What you see in the output is actually the two text 20 rows apart. Add in widgets, and you will see your result.

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

You can also use grid_rowconfigure, grid_columnconfigure and sticky properties in order to specify how a widget in a grid stretches. That way you can place your widgets in the right screen locations.

在这里查看我的答案以获取有关如何使用网格属性的更多详细信息:

Have a look at my answer here for more details on how to use grid properties: Tkinter. subframe of root does not show

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

To understand your grid better, I added another widget to you 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()

尝试一下.

PS:不需要在每行python之后添加;

PS: You do not need ; after every line python

这篇关于python中的Tkinter grid()对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆