【发布时间】:2016-10-13 23:45:02
【问题描述】:
我想运行一个命令行工具以在单独的函数中运行并传递给按钮,单击该程序的附加命令,但每次我都得到这个作为响应。
接受 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 有问题
【问题讨论】:
-
我们能看到确切的错误,以及它发生的具体行
-
´Traceback(最近一次调用最后):文件“*/subprocessExtra.py”,第 33 行,在
gui = CoreGUI(root) 文件“*/subprocessExtra.py”,第 18 行,在 init button = Button(self.parent, text="Check Device", command= self.adb("devices"))忽略在:<__main__.stdoutredirector object at> AttributeError: 'StdoutRedirector' object has no attribute 'flush' Process finished with exit code 1 ' -
这应该是问题:^)
-
看起来使用重定向器的代码需要
flush方法,但您没有提供。您是否尝试将flush方法添加到重定向器?您还错误地使用了command属性。见stackoverflow.com/q/5767228/7432 -
我尝试通过代码 sn-ps 来理解,并在他们学习如何编码时尝试。 lambda 不起作用
标签: python python-3.x tkinter subprocess popen