我一直在尝试在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>
答案 0 :(得分:1)
由于评论中已经怀疑 PM 2Ring ,您使用的是比例字体。只需导入pango并在init方法中添加(function(){
var imgLength = $('img').length;
var current = 1;
var currentImage = $('img');
$('button').on('click',function(){
if($(this).data('dir') === 'next')
{
current++;
currentImage.animate({
'left':'-=600px'
});
if(current > imgLength)
{
currentImage.animate({
'left':'0px'
});
current = 1;
}
}
if($(this).data('dir') === 'prev'){
current--;
currentImage.animate({
'left':'+=600px'
});
if(current < 1)
{
currentImage.animate({
'left': (600 * (1 - imgLength )) + 'px'
});
current = imgLength;
}
}
});
})();
即可。
但是,你应该注意到你的textview太小了,不能很好地显示表格,而且为了显示这样的数据,GtkListView和GtkTreeStore结合起来更适合。