When I run this code I get the error message:
File "Start.py", line 22, in
c.lo()
TypeError: lo() takes no arguments (1 given)
I don't know exactly why I am getting this error could someone please explain?
I know it's saying that I put an argument when calling that function but I don't understand why that is?
If someone could shed some light on this issue that would be great.
import subprocess as sp
import Tkinter as Tk
from Tkinter import *
root = Tk()
text = Text(root)
class Console:
def Start():
proc = sp.Popen(["java", "-Xmx1536M", "-Xms1536M", "-jar", ".jar"],stdin=sp.PIPE,stdout=sp.PIPE,)
def lo():
while True:
line = proc.stdout.readline()
text.insert(INSERT,line)
text.pack()
if (line == "Read Time Out"):
proc.stdin.write('stop')
if (line == "Unloading Dimension"):
text.insert(INSERT,"Ready for command")
text.pack()
c = Console()
c.Start()
c.lo()
root.mainloop()
解决方案
In short, that is because lo() is a method of the class Console which is always passed the instance as first argument. So lo() must define a parameter (mostly called self) to hold that argument:
class Console:
def start(self): # functions and methods should have lowercase names
self.proc = sp.Popen(...)
def lo(self):
line = self.proc.stdout.readline()
...
I am surprised that your Start() call worked; it has the same issue.