Ibrahim
Ibrahim

Reputation: 55

pygtk textview string formatting issue

I have been trying to print python formatted string as tabular data in pygtk textview, but output goes wrong. however when copy the output from textview control to notepad or gedit, it shows as formated output. print on terminal is fine as well.

can you please guide me how can i do this?

'''
Created on Sep 12, 2015

@author: ibrahim
'''
data = [
        ('Column A', 'Column B', 'Column C', 'Columnd D', 'Column E'), 
        ('4', 'ABC', 'Title of product 1', 1.65, -2.4), 
        ('2.99', 'MDT', 'Title of product 1 is here', -0.16, -2.3968000000000003), 
        ('2.85', 'LLY', 'here is another title of another product', 1.19, -1.4985000000000002), 
        ('2.77', 'ABBV', 'Google.com', -0.39, -1.4652000000000003), 
        ('2.71', 'CELG', 'Corporation work', 0.81, -1.1988), 
        ('2.66', 'MCK', 'not working examle', 1.3, -1.0803), 
        ('2.53', 'BIIB', 'I am stuck here', 0.88, -0.9177), 
        ('2.49', 'ABT', 'Abbott Laboratories', 0.67, -0.6596000000000001), 
        ('2.41', 'BMY', 'Steel Mills Company', 0.8, -0.5712)]


from tabulate import tabulate

print tabulate(data[1:], data[0],tablefmt='orgtbl')


import sys

try:  
    import pygtk  
    pygtk.require("2.0")  
except:  
    pass  

try:  
    import gtk  
except:  
    print("GTK Not Availible")
    sys.exit(1)


class MyClass(object):
    '''
    classdocs
    '''
    def __init__(self, builder):
        '''
        Constructor
        '''
        self.builder = builder
        builder.connect_signals(self)
        self.window  = builder.get_object('window1')
        self.textbuffer = self.textview.get_buffer()

        self.addRowToTextArea(tabulate(data[1:], headers=data[0], tablefmt='orgtbl'))

    def addRowToTextArea(self, text):
        position = self.textbuffer.get_end_iter()
        text += '\n'
        self.textbuffer.insert(position, text)

    def show_all(self):
        self.window.show_all()

    @property
    def textview(self):
        return self.builder.get_object("textview")

    def on_window1_destroy(self, *args):
        print "exiting application!"
        gtk.main_quit()

def run():
    builder = gtk.Builder()
    builder.add_from_file("ui.glade")
    sm = MyClass(builder)
    sm.show_all()

    gtk.main()

if __name__ == '__main__':
    run()

ui.glade file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Example</property>
    <property name="window_position">center-always</property>
    <signal name="destroy" handler="on_window1_destroy" swapped="no"/>
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkScrolledWindow" id="scrolledwindow1">
            <property name="width_request">470</property>
            <property name="height_request">380</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">automatic</property>
            <property name="vscrollbar_policy">automatic</property>
            <child>
              <object class="GtkTextView" id="textview">
                <property name="width_request">300</property>
                <property name="height_request">380</property>
                <property name="can_focus">True</property>
                <property name="border_width">5</property>
                <property name="editable">False</property>
                <property name="wrap_mode">word</property>
                <property name="justification">fill</property>
                <property name="right_margin">20</property>
                <property name="cursor_visible">False</property>
                <property name="accepts_tab">False</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="x">64</property>
            <property name="y">135</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label7">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="x">530</property>
            <property name="y">246</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Upvotes: 0

Views: 153

Answers (1)

elya5
elya5

Reputation: 2258

As PM 2Ring already suspected in the comments, you are using a proportional font. Simply importing pango and adding self.textview.modify_font(pango.FontDescription('monospace')) in your init method should work.

However, you should note that your textview is too small to show the table nicely and for displaying data like this a GtkListView in combination with a GtkTreeStore is better suited.

Upvotes: 1

Related Questions