0

Running Python 3.4.2 and Tkinter 8.6 on a Raspberry Pi. I want to create a Text widget that fills a fixed sized frame using the grid layout manager.

If you run the code below, you'll see the Text widget doesn't fill the 800x600 frame. I realize I could set the Text's width and height attributes to fill the frame, but this would only work default font and wouldn't fill the frame exactly.

from tkinter import *

class Test(Tk):
  def __init__(self):
    super().__init__()
    self.text = frameText(self)
    self.geometry('{}x{}'.format(800, 600))   

def frameText(frame, **kw):
  ysb = Scrollbar(frame)
  xsb = Scrollbar(frame, orient = HORIZONTAL)
  text = Text(frame, **kw)
  ysb.configure(command = text.yview)
  xsb.configure(command = text.xview)
  text.configure(yscrollcommand = ysb.set, xscrollcommand = xsb.set)
  text.grid(row = 0, column = 0, sticky = N+E+S+W)
  ysb.grid(row = 0, column = 1, sticky = N+S)
  xsb.grid(row = 1, column = 0, sticky = E+W)
  return text

if __name__ == '__main__':
  Test().mainloop()
0

1 Answer 1

1

You need to configure the grid to give the row and column a nonzero weight so that they expand

from tkinter import *

class Test(Tk):
  def __init__(self):
    Tk.__init__(self)
    self.grid_columnconfigure(0, weight=1)
    self.grid_rowconfigure(0, weight=1)
    self.text = frameText(self)
    self.geometry('{}x{}'.format(800, 600))   

def frameText(frame, **kw):
  ysb = Scrollbar(frame)
  xsb = Scrollbar(frame, orient = HORIZONTAL)
  text = Text(frame, **kw)
  ysb.configure(command = text.yview)
  xsb.configure(command = text.xview)
  text.configure(yscrollcommand = ysb.set, xscrollcommand = xsb.set)
  text.grid(row = 0, column = 0, sticky = N+E+S+W)
  ysb.grid(row = 0, column = 1, sticky = "ns")
  xsb.grid(row = 1, column = 0, sticky = E+W)
  return text

if __name__ == '__main__':
  Test().mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.