python中对齐问题_python中的Tkinter grid()对齐问题

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!

CODE:

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 :-(

解决方案

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.

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

Try this.

PS: You do not need ; after every line python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值