2015-08-28 44 views
0

我正在嘗試使用Python類創建一個GUI。由於我是Python新手,我仍然在學習排查錯誤。下面,我希望創建一個名爲Plot_Seismo的類,並創建一個具有列表框,退出按鈕和「繪圖」按鈕的GUI。我在課堂上的「Plot_button」遇到問題。我想要這個按鈕做的是在地震記錄中讀取,然後從列表框中繪製選定的地震圖。我有一種感覺,我的語法不正確(由於我的天真)。當這個函數不在類中時,我可以得到這個工作,但是當我把它作爲一個方法放在一個類中時,我會感到有些困惑。該錯誤消息如下所示:Python中的方法和類錯誤

#!/usr/bin/env python 
from Tkinter import * 
from obspy.core import read 
import math 


class Plot_Seismo: 
    def __init__(self, parent): 
     self.master = parent 
     top = Frame(parent, width=500, height=300) 
     top.pack(side='top') 

     # create frame to hold the first widget row: 
     hwframe = Frame(top) 
     # this frame (row) is packed from top to bottom (in the top frame): 
     hwframe.pack(side='top') 
     # create label in the frame: 
     font = 'times 18 bold' 
     hwtext = Label(hwframe, text='Seismogram Reader GUI', font=('Arial',18,'bold'), fg="red") 
     hwtext.pack(side='top') 

     ### ListBox 
     List1 = Listbox(root, width=50, height= 10) 
     List1.insert(1,"trace1.BHZ") 
     List1.insert(2,"trace2.BHZ") 
     List1.pack(padx=20, pady=20) 


     plot_button = Button(top, text='Plot Seismogram', command=self.plot_seis) 
     plot_button.pack(side='top', anchor='w', padx=45, pady=20) 
     self.event = read(List1.get(List1.curselection()[0])) 

     # finally, make a quit button and a binding of q to quit: 
     quit_button = Button(top, text='Quit Seismo-Reader GUI', command=self.quit) 
     quick_button.pack(side='top', anchor='w', padx=20, pady=20) 
     self.master.bind('<q>', self.quit) 

    def quit(self, event=None): 
     self.master.quit() 

    def plot_seis(self, event=None): 
     self.event.plot() 

root = Tk() 
Plot_Seismo = Plot_Seismo(root) 
root.mainloop() 

Error Message: 
Traceback (most recent call last): 
    File "plot_seismogram.py", line 46, in <module> 
    Plot_Seismo = Plot_Seismo(root) 
    File "plot_seismogram.py", line 31, in __init__ 
    self.event = read(List1.get(List1.curselection()[0])) 
IndexError: tuple index out of range 
+0

你得到一個錯誤信息?或者按鈕只是無所事事? – albert

+0

我在上面添加了錯誤消息。我希望先閱讀事件(從列表中選擇),然後使用plot_seis函數使用plot_button繪製地震圖。謝謝。 –

回答

3

由於我沒有安裝obspy模塊,我不得不收縮下此驗證碼了一點,但你應該明白了吧。

因爲我只在我的機器上運行Python3,所以我重寫了Python3的代碼,這不是什麼大問題。唯一的區別應該是(tkinter而不是Tkinterprint()而不是print)。

我更改了代碼的某些部分:列表框沒有使用列表填充,這使得這更容易一些,並且它變成了訪問plot_seis的類屬性。

由於.curselection()返回與列表框項索引元組,我們必須get根據該文本輸入作爲docsthis answer描述。

按鈕,也許列表框提供一些事件通過使用self.種醜陋莫名其妙地處理這些都使得列表框的類屬性的功能,但它的工作:

#!/usr/bin/env python3 
# coding: utf-8 

from tkinter import * 
# from obspy.core import read 
import math 


class Plot_Seismo: 
    def __init__(self, parent): 
     self.master = parent 
     top = Frame(parent, width=500, height=300) 
     top.pack(side='top') 

     # create frame to hold the first widget row: 
     hwframe = Frame(top) 
     # this frame (row) is packed from top to bottom (in the top frame): 
     hwframe.pack(side='top') 
     # create label in the frame: 
     font = 'times 18 bold' 
     hwtext = Label(hwframe, text='Seismogram Reader GUI', font=('Arial',18,'bold'), fg="red") 
     hwtext.pack(side='top') 

     ### ListBox 
     self.List1 = Listbox(root, width=50, height= 10) 
     # populate listbox using a list with desired entries 
     self.list_entries = ["trace1.BHZ", "trace2.BHZ"] 
     for i, element in enumerate(self.list_entries): 
      self.List1.insert(i, element) 
     self.List1.pack(padx=20, pady=20) 

     plot_button = Button(top, text='Plot Seismogram', command=self.plot_seis) 
     plot_button.pack(side='top', anchor='w', padx=45, pady=20) 

     # finally, make a quit button and a binding of q to quit: 
     quit_button = Button(top, text='Quit Seismo-Reader GUI', command=self.quit) 
     quit_button.pack(side='top', anchor='w', padx=20, pady=20) 
     self.master.bind('<q>', self.quit) 

    def quit(self, event=None): 
     self.master.quit() 

    def plot_seis(self, event=None): 
     selection_index = self.List1.curselection()[0] 
     selection_text = self.List1.get(selection_index) 
     print(selection_text) 

     # do something with `read` from the `obspy.core` module 
     # read(selection_text) 


root = Tk() 
Plot_Seismo = Plot_Seismo(root) 
root.mainloop() 
+0

嗨,阿爾伯特,這工作!它看起來像我掛在列表框上,忘了添加'自我'前綴,這是與地震記錄按鈕搞砸了。我也很感謝你增加了for循環,這使代碼更具可讀性。再次感謝您的幫助!我很感激。 –