GUI错误中的Python方法和类

时间:2015-08-28 22:18:24

标签: python tkinter

我正在尝试使用Python类创建GUI。因为我是Python的新手,我还在学习解决我的错误。下面,我希望创建一个名为Plot_Seismo的类,并创建一个具有列表框,退出按钮和" Plot"按钮。我在使用" Plot_button"在课堂上。我想要这个按钮做的是在地震图中读取,然后从列表框中绘制选定的地震图。我有一种感觉我的语法不正确(由于我的天真)。当这个函数不在类中时,我可以让它工作,但当我把它作为一个方法放在一个类中时,我会有点困惑。错误消息如下所示:

#!/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

1 个答案:

答案 0 :(得分: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()