接受1个位置参数,但给出了2个 [英] takes 1 positional argument but 2 were given
问题描述
我想运行一个命令行工具以在单独的函数中运行并传递给按钮,请单击该程序的附加命令,但是每次我得到它作为响应时.
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.
接受1个位置参数,但给出2个位置
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()
错误
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
有些身体可以帮助我
** args有问题
there is something wrong with **args
推荐答案
这是因为您在此处提供了位置参数:
It is because you are providing it a positional argument here:
button = Button(self.parent, text="Check Device", command= self.adb("devices"))
命令想要一个回调函数.并且您将adb
方法的响应传递给它. (有关更多信息,请参见此处: http://effbot.org/tkinterbook/button.htm )
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)
在调用该行时,在调用self.adb("devices")
.
如果您查看adb
when that line is being called, self.adb("devices")
is being called.
if you look at your definition of adb
def adb(self, **args):
您只需要1个位置参数self
和任意数量的关键字参数**args
,然后用2个位置参数self
和"devices"
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"
如果要使adb
方法更通用,或者将"devices"
放入adb
方法,您将需要一个中间方法.
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.
修改
另请参见此处: http://effbot.org/zone/tkinter-callbacks.htm 参见将参数传递给回调"部分
See also here: http://effbot.org/zone/tkinter-callbacks.htm See the section "Passing Argument to Callbacks"
修改2:代码示例
如果您这样做,它应该可以工作:
If you do this, it should work:
button = Button(self.parent, text="Check Device", command=lambda: self.adb("devices"))
,然后将您的功能更改为**
的单个*
位置(关键字arg扩展),请参见此处: https: //stackoverflow.com/a/36908/6030424 了解更多说明.
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)
这篇关于接受1个位置参数,但给出了2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!