pygtk textview 字符串格式化问题
pygtk textview string formatting issue
我一直在尝试将 python 格式的字符串打印为 pygtk 文本视图中的表格数据,但输出出错了。但是,当将输出从 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 太小,无法很好地显示 table,对于显示这样的数据,GtkListView 与 GtkTreeStore 的组合更适合。
我一直在尝试将 python 格式的字符串打印为 pygtk 文本视图中的表格数据,但输出出错了。但是,当将输出从 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 太小,无法很好地显示 table,对于显示这样的数据,GtkListView 与 GtkTreeStore 的组合更适合。