我正在尝试编写一个函数来将文本写入应用程序内的终端窗口。但我得到了上述错误。
这是我的一些代码。这是播放midi文件的GUI。我试图在终端窗口中显示打开的文件:
import pygtk
pygtk.require('2.0')
import gtk
import signal
import pango
import subprocess
textview = gtk.TextView()
class Teacher:
result = ""
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(400, 200)
window.set_title("The Improvisor")
window.connect("delete_event",
lambda w,e: gtk.main_quit())
table = gtk.Table(4, 4, True)
window.add(table)
button1 = gtk.Button("Open")
button1.connect("clicked", self.clicked_open_file)
button1.show()
button2 = gtk.Button("Play")
button2.connect("clicked", self.clicked_play)
button2.show()
button3 = gtk.Button("Stop")
button3.connect("clicked", self.clicked_stop)
button3.show()
fontdesc = pango.FontDescription('monospace')
textview.modify_font(fontdesc)
scroll = gtk.ScrolledWindow()
scroll.add(textview)
textview.show()
table.attach(button1, 0, 1, 0, 1)
table.attach(button2, 0, 1, 1, 2)
table.attach(button3, 0, 1, 2, 3)
table.attach(button4, 0, 4, 3, 4)
table.attach(scroll, 1, 4, 0, 3)
window.show_all()
def clicked_play(self, widget):
result = self.result
if result == "":
parent = None
alert = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO,
gtk.BUTTONS_CLOSE, "Please Select a File")
alert.run()
alert.destroy()
else :
self.proc = subprocess.Popen(["timidity", result])
def clicked_open_file(self, widget):
chooser = gtk.FileChooserDialog(title="Open a file",action=gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
response = chooser.run()
self.result = chooser.get_filename()
self.insert_text(self.result)
chooser.destroy()
def clicked_stop(self, widget=None):
if self.proc:
self.proc.terminate()
self.proc.wait()
def insert_text(text):
textview.get_buffer().insert_at_cursor(text)
def main(self):
signal.signal(signal.SIGTERM, self.clicked_stop)
gtk.main()
return 0
Teacher().main()
答案 0 :(得分:1)
您的insert_text
方法缺少self
属性。将其更改为:
def insert_text(self, text):
...
请记住:Python中的self
参数是类的当前实例,必须明确声明为方法的第一个参数。