簡體   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