0

I cannot get my widgets fit in my computer screen and the layout is not forming as expected. The two Text widgets should expand and occupy rest of frame available to them and the second frame which contains response2Field should fit into screen but it's not happening. How can I achieve these goals?

# -*- coding: utf-8 -*-
from Tkinter import Tk,Label,Entry,Text,Button,Frame

text = """Python was created in the early 1990s by Guido van Rossum at Stichting
Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
as a successor of a language called ABC.  Guido remains Python's
principal author, although it includes many contributions from others."""
root = Tk() 
request = Frame(root)
response =  Frame(root)
response1 = Frame(response)
response2 = Frame(response)
request.grid(row=0,column=0,sticky='news')
response.grid(row=0,column=1)
response1.grid(row=0,column=0,sticky='w')
response2.grid(row=1,column=0,sticky='news')

InputText = Text(request)
InputText.pack(expand='true')
Button(response1,text='Submit',bg='brown',fg='yellow').pack()
response2Field = Text(response2,bg='black',fg='green')
response2Field.pack(expand='true')
InputText.insert("1.0",text)
response2Field.insert("1.0",text)
root.mainloop()

Output: Result

Hritik
  • 673
  • 1
  • 6
  • 24

1 Answers1

2

The default behaviors of tkinter geometry managers pack and grid is to grow containers and windows to display everything you put inside. If you want to constrain this, you can add at the end of your gui building code (copied from this other answer)

root.geometry('{}x{}'.format(<widthpixels>, <heightpixels>))

To have this to work, you have to handle correctly resizing in your layout. First of all, your use of grid is overelaborate, you do not have to use all these intermediate frames and can put your widgets directly in the grid. Second, grid need to be instructed of which row and columns should grow. This is done through the weight parameter. It describes the growth rate of an element (relative to the sum of all weights at the same level) and default to 0. This is configured on the container side. For instance, to have your request and response fill the entire height of the window, you have to add

root.grid_rowconfigure(0, weight=1)

On pack side, you have to specify both parameter expand and fill to have your widgets fill the entire available space pack(expand='true', fill='both').

To visualize re-size behavior of your Frame containers, you might consider to add borders borderwidth=2, relief='sunken' or background background='magenta' (it hurts the eyes, but helps to understand).

same program with relief and colored frame

You can see that indeed InputText is not resizing, neither magenta request. response2Field occupies its whole green frame (misses fill='both' for proper resize handling, but not visible since part of the path which determine window original size).

Community
  • 1
  • 1
FabienAndre
  • 4,514
  • 25
  • 38
  • Your language seems to be confusing. Can you post your code and address my problems point-wise? – Hritik Mar 28 '15 at 15:55
  • My answer details 3 steps needed to reach your goals: (1) limit tk window size (2) handle resize in grid (3) handle resize in pack. I provide the code for (1) . For (2), you have to specify a weight for all the columns or rows you want to see growing. And for (3), you have to add `fill` argument when you pack your `Text`s widget. – FabienAndre Mar 28 '15 at 19:39