Search code examples
pythontkintertk-toolkit

Python tkinter how to use grid sticky


import tkinter as tk
root = tk.Tk()
root.state('zoomed')
text1 = tk.Text(state=tk.DISABLED)

scroll = tk.Scrollbar(root, command=text1.yview)
text1.configure(yscrollcommand=scroll.set)
text1.grid(row=1, column=1, sticky='we')
scroll.grid(row=1, column=2, sticky="nse")

root.mainloop()

How do I make the Text box fill the X axis? (sticky='we' did not work for me)


Solution

  • import tkinter as tk
    root = tk.Tk()
    root.state('zoomed')
    
    text1 = tk.Text(state=tk.DISABLED)
    scroll = tk.Scrollbar(root, command=text1.yview)
    text1.configure(yscrollcommand=scroll.set)
    
    text1.grid(row=1, column=0,sticky='we')
    scroll.grid(row=1, column=1, sticky="ns")
    
    root.grid_columnconfigure(0, weight=1)
    root.grid_columnconfigure(1, weight=0)
    
    root.mainloop()
    

    I've added the parameter weight to the columns in that grid, which basically is a relationship between the columns. Please see, What does 'weight' do in tkinter? to get a more detailed answer.