简体   繁体   中英

Python tkinter set text widget to bottom of window using grid

I am totally new to using tkinter and am playing around with grid currently. I have set my window to be 1000x500 and have an image at the top left like this...

window = Tk()
window.geometry("1000x500") #Width x Height

logo = PhotoImage(file="logo.gif")
Label (window, image=logo, bg="#f0f0f0") .grid(row=0, column=0)

T = Text(window, height=2, width=30)
T.insert(END, "Just a text Widget\nin two lines\n")
T.grid(row=2, column=0)

I would like the text widget to be placed at the bottom of the window, I have tried setting the row to something larger but it doesn't have any effect.

Where am I going wrong?

Apparently,The easiest way is to use .pack() instead of .grid() .But if you really want to use .grid() .You need to set the rowconfigure() to set the weight of row,And sticky="s" or sticky=S to make it in the bottom.

Your code can be:

from tkinter import *

window = Tk()
# window.geometry("1000x500") #Width x Height

logo = PhotoImage(file="xxx")
Label (window, image=logo, bg="#f0f0f0") .grid(row=0, column=0)

T = Text(window, height=2, width=30)
T.insert(END, "Just a text Widget\nin two lines\n")
T.grid(row=1, column=0, sticky=S)

window.grid_rowconfigure(1,weight=1)

window.mainloop()

Remember,if you want to always make it in the bottom and only use .gird() ,you need to set the row weight of the text.So I suggest you put all the widget(except the Text widget in the bottom) in a Frame .And use .grid() in the Frame .The Frame and the Text use .pack() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM