2017-02-04 72 views
0

我是新手,想用python開發GUI。我的數據是一個.sgy文件,我需要顯示圖形作爲輸出。我選擇了Pyglet這樣做。這是一個正確的選擇嗎?我在第一步感到震驚,因爲我想在窗口的標籤中顯示地震數據的標題。這裏是我已經試過代碼:在pyglet中顯示標籤的動態值

import pyglet  
import sys  
import numpy as np   #package for numerical processing  
import matplotlib    #package for plotting graphs  
import obspy     #package for reading sgy file 
import pandas     #package for handling csv file 
from obspy.io.segy.segy import _read_segy  
from obspy.core.util import get_example_file  


window = pyglet.window.Window()  

filename = get_example_file("/home/khyati/Downloads/FindTrappedMiners.SGY") 

st = _read_segy(filename)  

for i in st.traces:  
    label = pyglet.text.Label(i, 
          font_name='Times New Roman',  
          font_size=36, 
          x=window.width//2, 
          y=window.height//2, 
          anchor_x='center', anchor_y='center') 


@window.event  
def on_draw():  
    window.clear()  
    label.draw()  

pyglet.app.run() 

這裏是我遇到

TypeError         Traceback (most recent call last)  
<ipython-input-32-869a2d62b820> in <module>()  
    21       x=window.width//2, 
    22       y=window.height//2,  
---> 23       anchor_x='center', anchor_y='center') 
    24  
    25 

/home/khyati/anaconda2/lib/python2.7/site-packages/pyglet-1.2.4-py2.7.egg/pyglet/text/__init__.py in __init__(self, text, font_name, font_size, bold, italic, color, x, y, width, height, anchor_x, anchor_y, align, multiline, dpi, batch, group)  
    428  
    429   '''  
--> 430   document = decode_text(text) 
    431   super(Label, self).__init__(document, x, y, width, height, 
    432          anchor_x, anchor_y,  

/home/khyati/anaconda2/lib/python2.7/site-packages/pyglet-1.2.4-py2.7.egg/pyglet/text/__init__.py in decode_text(text) 
    210  ''' 
    211  decoder = get_decoder(None, 'text/plain')  
--> 212  return decoder.decode(text) 
    213 
    214 class DocumentLabel(layout.TextLayout):  

/home/khyati/anaconda2/lib/python2.7/site-packages/pyglet-1.2.4-py2.7.egg/pyglet/text/formats/plaintext.py in decode(self, text, location) 
    44  def decode(self, text, location=None): 
    45   document = pyglet.text.document.UnformattedDocument()  
---> 46   document.insert_text(0, text) 
    47   return document  

/home/khyati/anaconda2/lib/python2.7/site-packages/pyglet-1.2.4-py2.7.egg/pyglet/text/document.py in insert_text(self, start, text, attributes)  
    422  
    423   '''  
--> 424   self._insert_text(start, text, attributes)  
    425   self.dispatch_event('on_insert_text', start, text) 
    426  

/home/khyati/anaconda2/lib/python2.7/site-packages/pyglet-1.2.4-py2.7.egg/pyglet/text/document.py in _insert_text(self, start, text, attributes)  
    426  
     427  def _insert_text(self, start, text, attributes):  
    --> 428   self._text = u''.join((self._text[:start], text, self._text[start:]))  
     429   len_text = len(text)  
     430   for element in self._elements:  

    TypeError: sequence item 1: expected string or Unicode, SEGYTrace found  

任何幫助,將不勝感激的錯誤。

+0

閱讀錯誤信息:什麼是'我'?一些痕跡。什麼是痕跡?不是字符串。但標籤需要字符串作爲第一個參數。所以也許你需要'str(i)'或者它可能有'i.text'或類似的東西。 – furas

回答

0

根據ObsPy documentationst中有每個元素的stats值。

由於st本身似乎是一組數據,而您只是簡單地提取每個數據的ID,所以您有兩個或三個選項。

一個,是,如果你要打印的ID作爲圖形標籤,然後做什麼@furas說:

for i in st.traces: 
    label = pyglet.text.Label(str(i), 
         x=window.width//2, 
         y=window.height//2, 
         anchor_x='center', anchor_y='center') 

或者,您可以通過兩種方式提取要顯示的數據:

label = pyglet.text.Label(st.traces[i].data, ...) 

label = pyglet.text.Label(st[i].stats.segy.trace_header, ...) 

無論哪種方式,您的主要問題是pyglet.text.Labelstr,因爲它的參數是text。在那裏你可以插入一個整數作爲第一個參數。轉換您的int或使用文本表示的跟蹤文件中的數據。

另請注意,通過執行for i in x: label = <insert label>您將替換每個循環中的標籤,並且您正在執行此功能以外的功能,這意味着您只會基本上創建一個標籤。也許你已經明白了這一點,或者是有意的,但值得一提。我從來沒有與obspy合作,我剛剛與Pyglet合作過,所以也許有人對obspy有更多的經驗會糾正我使用這些數據。

+0

感謝您的幫助,但是使用您提到的任何一種方式,我遇到了使用第二種方法時出現的某些錯誤,遇到的錯誤是:TypeError Traceback(最近調用最後一次) () 15對於i在st.traces: ---> 16標記= pyglet.text.Label(ST [I] .stats.segy.trace_header, 17 font_name首='時報New Roman', 18 font_size = 36, TypeError:'SEGYFile'對象沒有屬性'__getitem__' –

+0

另外,因爲我是使用Python進行編程的全新手段,我想知道向前移動Pyglet是否是一個好選擇?我需要開發非常複雜的圖形用戶界面,還有什麼其他方法可以開發GUI,包括像Visual Studio一樣的拖放小部件。 Thanxx提前! –

+0

你使用我的代碼嗎?你是通過某種IDE運行它嗎?因爲錯誤輸出(回溯)對我來說看起來有點時髦。這取決於你想要做什麼樣的UI。我會說,如果你只是想要按鈕和標籤,你最好用wxPython或者任何你覺得足夠花哨的庫。但是這個問題並不適合於stackoverflow(使用什麼UI庫),因爲我們通常不會允許基於意見的問題。我可以幫助你解決這個問題。但是你使用了什麼代碼,因爲我從來沒有使用'getitem'。除非'trace_header',但是整個棧板 – Torxed