簡體   English   中英

pygtk textview字符串格式問題

[英]pygtk textview string formatting issue

我一直試圖在pygtk textview中將python格式的字符串作為表格數據打印,但是輸出出錯。 但是,將輸出從textview控件復制到記事本或gedit時,它顯示為格式化輸出。 在終端上打印也很好。

你能指導我怎么做嗎?

'''
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文件如下

<?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>

由於注釋中已經懷疑PM 2Ring ,因此您使用的是比例字體。 只需導入pango並在init方法中添加self.textview.modify_font(pango.FontDescription('monospace'))

但是,您應該注意,您的textview太小,無法很好地顯示該表,並且對於將GtkListView與GtkTreeStore結合使用這樣的數據顯示更為合適。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM