我正在嘗試使用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
你得到一個錯誤信息?或者按鈕只是無所事事? – albert
我在上面添加了錯誤消息。我希望先閱讀事件(從列表中選擇),然後使用plot_seis函數使用plot_button繪製地震圖。謝謝。 –