2014-10-06 89 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列中沒有小部件,所以在其他行和列中放置一些東西你的標籤會移動。