takes 1 positional argument but 2 were given

2024/10/9 10:29:32

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 subprocessclass StdoutRedirector(object):def __init__(self,text_widget):self.text_space = text_widgetdef write(self,string):self.text_space.insert('end', string)self.text_space.see('end')class CoreGUI(object):def __init__(self,parent):self.parent = parentself.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

Answer

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)
https://en.xdnf.cn/q/70032.html

Related Q&A

With py.test, database is not reset after LiveServerTestCase

I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py. This Test Case has uses the LiveServerTestCase and StaticLiveSer…

Using flask wtforms validators without using a form

Im receiving user registration data from an iOS application and Id like to use the validators that come with wtforms to make sure the email and password are acceptable. However, Im not using a flask f…

How to install graph-tool for Anaconda Python 3.5 on linux-64?

Im trying to install graph-tool for Anaconda Python 3.5 on Ubuntu 14.04 (x64), but it turns out thats a real trick.I tried this approach, but run into the problem:The following specifications were foun…

How to quickly encrypt a password string in Django without an User Model?

Based on my current Django app settings, is there a function or a snippet that allows me to view the encrypted password, given a raw string? I am testing some functionality and this would be useful fo…

Embedding multiple gridspec layouts on a single matplotlib figure?

I am using the python graphing library matplotlib to graph several things in a report, and I found myself needing to have several fixed-count graphs above an arbitrary grid of smaller graphs. I search…

Writing integers in binary to file in python

How can I write integers to a file in binary in Python 3?For example, I want to write 6277101735386680763835789423176059013767194773182842284081 to a file in binary in exactly 24 bytes (unsigned, I wi…

Dropping some columns when using to_csv in pandas

I have a data frame which I want to write to tow files, one that contains all of the columns and one that has only a subset of the columns So for this data frame: Out_dataOut[9]: A B …

Setting Max Results in API v4 (python)

In v3 of the API Im seeing that there was a max-results parameter that could be passed to get more than 1000 records. I havent been able to figure out how to pass that parameter in v4 of the API using …

Extract text between two different tags beautiful soup

Im trying to extract the text content of the article from this web page.Im just trying to extract the article content and not the "About the author part".The problem is that all the content a…

Add column to pandas without headers

How does one append a column of constant values to a pandas dataframe without headers? I want to append the column at the end.With headers I can do it this way:df[new] = pd.Series([0 for x in range(le…