I would like to run a command line tool to run in a separate function and passed to the button click the additional command for this program but each time I get this as a response.
takes 1 positional argument but 2 were given
from tkinter import *
import subprocess
class StdoutRedirector(object):
def __init__(self,text_widget):
self.text_space = text_widget
def write(self,string):
self.text_space.insert('end', string)
self.text_space.see('end')
class CoreGUI(object):
def __init__(self,parent):
self.parent = parent
self.InitUI()
button = Button(self.parent, text="Check Device", command= self.adb("devices"))
button.grid(column=0, row=0, columnspan=1)
def InitUI(self):
self.text_box = Text(self.parent, wrap='word', height = 6, width=50)
self.text_box.grid(column=0, row=10, columnspan = 2, sticky='NSWE', padx=5, pady=5)
sys.stdout = StdoutRedirector(self.text_box)
def adb(self, **args):
process = subprocess.Popen(['adb.exe', args], stdout=subprocess.PIPE, shell=True)
print(process.communicate())
#return x.communicate(stdout)
root = Tk()
gui = CoreGUI(root)
root.mainloop()
the error
Traceback (most recent call last):
File "C:/Users/Maik/PycharmProjects/Lernen/subprocessExtra.py", line 33, in <module>
gui = CoreGUI(root)
File "C:/Users/Maik/PycharmProjects/Lernen/subprocessExtra.py", line 18, in __init__
button = Button(self.parent, text="Check Device", command= self.adb("devices"))
TypeError: adb() takes 1 positional argument but 2 were given
Exception ignored in: <__main__.StdoutRedirector object at 0x013531B0>
AttributeError: 'StdoutRedirector' object has no attribute 'flush'
Process finished with exit code 1
can some body help me
there is something wrong with **args
To solve this ” Typeerror: takes 1 positional argument but 2 were given ” is by adding self argument for each method inside the class. It will remove the error.
The Python "TypeError: takes 1 positional argument but 2 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a second argument in a function's definition. Passing two arguments to a function that only takes one.
The Python "TypeError: __init__() missing 1 required positional argument" occurs when we forget to provide a required argument when instantiating a class. To solve the error, specify the argument when instantiating the class or set a default value for the argument.
The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.
It is because you are providing it a positional argument here:
button = Button(self.parent, text="Check Device", command= self.adb("devices"))
command want's a callback function. and you are passing it the response from the adb
method. (see here fore more: http://effbot.org/tkinterbook/button.htm)
when that line is being called, self.adb("devices")
is being called.
if you look at your definition of adb
def adb(self, **args):
You are only asking for 1 positional argument self
and any number of keyword arguments **args
then you are calling it self.adb("devices")
with 2 positional arguments of self
and "devices"
What you will need to do is have an intermediate method, if you want to have the adb
method more general, or just put "devices"
into the adb
method.
edit
See also here: http://effbot.org/zone/tkinter-callbacks.htm See the section "Passing Argument to Callbacks"
edit 2: code example
If you do this, it should work:
button = Button(self.parent, text="Check Device", command=lambda: self.adb("devices"))
and then change your function to a single *
inlieu of a **
(keyword arg expansion) See here: https://stackoverflow.com/a/36908/6030424 for more explanation.
def adb(self, *args):
process = subprocess.Popen(['adb.exe', args], stdout=subprocess.PIPE, shell=True)
print(process.communicate())
#return x.communicate(stdout)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With