2014-10-06 146 views
0

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

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

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

 Issue with the grid.

+0

什么是你想要的输出后? – 2014-10-06 20:46:07

+0

@Luke Willis那么第二个标签(蓝色)应该向下放置,因为它向下20行,因为它是20列而不是第0列,所以应该更多地移动到屏幕中间 – 2014-10-06 20:48:15

+0

你能做什么做的是让空'tk.Label()'来填补空白,这可能对你有帮助 – W1ll1amvl 2014-10-06 23:57:16

回答

4

如果行或列不包含任何内容,那么它的大小为1像素,它非常小。你在输出中看到的实际上是两行相隔20行。添加小部件,你会看到你的结果。

您还可以使用grid_rowconfigure,grid_columnconfiguresticky属性来指定网格中窗口小部件的展开方式。这样你可以将你的小部件放置在正确的屏幕位置。

看一看我的答案在这里,了解如何使用网格属性的详细信息:Tkinter. subframe of root does not show

要了解您的网格更好,我增加了一个小工具,你的代码:

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:你不需要;每一行蟒蛇

3

空行有因为在行1-19中没有小部件,而在0-19列中没有小部件,所以在其他行和列中放置一些东西你的标签会移动。